benchmarking

How to benchmark a single function call in Python?

How to benchmark a single function call in Python? Question: How to interactively (micro)benchmark a code expression in Python (i.e. the equivalent of @btime/@benchmark in Julia) ? I would like to benchmark interactively an expression (a function call), where the number of times this expression should be evaluated depends on its computational cost / variance…. …

Total answers: 1

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers? Question: The following Python 3.x integer multiplication takes on average between 1.66s and 1.77s: import time start_time = time.time() num = 0 for x in range(0, 10000000): # num += 2 * (x * …

Total answers: 4

Measure (max) memory usage with IPython—like timeit but memit

Measure (max) memory usage with IPython—like timeit but memit Question: I have a simple task: in addition to measuring the time it takes to execute a chunk of code in Python, I need to measure the amount of memory a given chunk of code needs. IPython has a nice utility called timeit which works like …

Total answers: 1

Python Requests vs PyCurl Performance

Python Requests vs PyCurl Performance Question: How does the Requests library compare with the PyCurl performance wise? My understanding is that Requests is a python wrapper for urllib whereas PyCurl is a python wrapper for libcurl which is native, so PyCurl should get better performance, but not sure by how much. I can’t find any …

Total answers: 4

Python Numpy Data Types Performance

Python Numpy Data Types Performance Question: So I did some testing and got odd results. Code: import numpy as np import timeit setup = “”” import numpy as np A = np.ones((1000,1000,3), dtype=datatype) “”” datatypes = “np.uint8”, “np.uint16”, “np.uint32”, “np.uint64”, “np.float16”, “np.float32”, “np.float64” stmt1 = “”” A = A * 255 A = A / …

Total answers: 2

benchmarks: does python have a faster way of walking a network folder?

benchmarks: does python have a faster way of walking a network folder? Question: I need to walk through a folder with approximately ten thousand files. My old vbscript is very slow in handling this. Since I’ve started using Ruby and Python since then, I made a benchmark between the three scripting languages to see which …

Total answers: 2

Interpreting a benchmark in C, Clojure, Python, Ruby, Scala and others

Interpreting a benchmark in C, Clojure, Python, Ruby, Scala and others Question: Disclaimer I know that artificial benchmarks are evil. They can show results only for very specific narrow situation. I don’t assume that one language is better than the other because of the some stupid bench. However I wonder why results is so different. …

Total answers: 13

Why does Python code run faster in a function?

Why does Python code run faster in a function? Question: def main(): for i in xrange(10**8): pass main() This piece of code in Python runs in (Note: The timing is done with the time function in BASH in Linux.) real 0m1.841s user 0m1.828s sys 0m0.012s However, if the for loop isn’t placed within a function, …

Total answers: 3

Why is matrix multiplication faster with numpy than with ctypes in Python?

Why is matrix multiplication faster with numpy than with ctypes in Python? Question: I was trying to figure out the fastest way to do matrix multiplication and tried 3 different ways: Pure python implementation: no surprises here. Numpy implementation using numpy.dot(a, b) Interfacing with C using ctypes module in Python. This is the C code …

Total answers: 6

Why is splitting a string slower in C++ than Python?

Why is splitting a string slower in C++ than Python? Question: I’m trying to convert some code from Python to C++ in an effort to gain a little bit of speed and sharpen my rusty C++ skills. Yesterday I was shocked when a naive implementation of reading lines from stdin was much faster in Python …

Total answers: 8