Dec-01-2023, 09:13 AM
Why so cagey?
Tell us what the functions and variables are!
As the guys said, np is faster than Python.
For this example, from realpython.com, numpy is nearly 100 times faster:
Tell us what the functions and variables are!
As the guys said, np is faster than Python.
Quote:It is sometimes said that Python, compared to low-level languages such as C++, improves development time at the expense of runtime.
For this example, from realpython.com, numpy is nearly 100 times faster:
import numpy as np from timeit import timeit def profit(prices): max_px = 0 min_px = prices[0] for px in prices[1:]: min_px = min(min_px, px) max_px = max(px - min_px, max_px) return max_px def profit_with_numpy(prices): """Price minus cumulative minimum price, element-wise.""" prices = np.asarray(prices) return np.max(prices - cummin(prices)) cummin = np.minimum.accumulate # compare speeds seq = np.random.randint(0, 100, size=100000) setup = ('from __main__ import profit_with_numpy, profit, seq;' ' import numpy as np') num = 250 pytime = timeit('profit(seq)', setup=setup, number=num) nptime = timeit('profit_with_numpy(seq)', setup=setup, number=num) print('Speed difference: {:0.1f}x'.format(pytime / nptime))
Output:Speed difference: 93.5x