code big_array= np.random.rand(1000000) %timeit sum(big_array)
this code above is done in jupyter notebook how do I use this code in idle
code big_array= np.random.rand(1000000) %timeit sum(big_array)
this code above is done in jupyter notebook how do I use this code in idle
The most direct is to build the timer into your Python code and then run it using IDLE based on here:
Using IDLE as you do run this based on the code you provided:
import time
start = time.process_time()
import numpy as np
big_array= np.random.rand(1000000)
sum(big_array)
print(time.process_time() - start)
If you wanted to use Pyinstrument (see below section), that is possible, too. The documentation suggests with your code that would be:
from pyinstrument import Profiler
profiler = Profiler()
profiler.start()
import numpy as np
big_array= np.random.rand(1000000)
sum(big_array)
profiler.stop()
profiler.print()
(I'm adding this because this is the more mature route. You won't really want weave the time/profiling into your code as you start develop more capable code and functions. And you probably won't use IDLE much down the road.)
The easiest is to make your code a script and then time it using pure Python and not Jupyter/IPython magics. I gather based on your code provided, you'd save your script, my_script.py
with the following contents:
import numpy as np
big_array= np.random.rand(1000000)
sum(big_array)
I tried using the cProfiler
suggested here and wasn't having much luck adapting that code example, and so I used a more modern profiler, pyinstrument, I had seen suggested by somebody much more knowledgeable about Python than myself. (Later, saw here I could just use python -m cProfile my_script.py
to use the cProfiler, but after using pyinstrument first I could quickly see why pyinstrument was recommended.)
I installed that easily with pip install pyinstrument
. And then ran the following on the command line/terminal:
pyinstrument my_script.py
That's meant to be the equivalent of running python my_script.py
on your command line/terminal, now with wrapping it in the pyinstrument profiling, see the documentation.
You'll see lots of profiling information including the time:
jovyan@jupyter-binder-2dexamples-2drequirements-2dfg42zx6b:~$ pyinstrument my_script.py_ ._ __/__ _ _ _ _ _/_ Recorded: 15:05:41 Samples: 96/_//_/// /_\ / //_// / //_'/ // Duration: 0.424 CPU time: 0.416
/ _/ v4.1.1Program: my_script.py0.423 <module> <string>:1[4 frames hidden] <string>, runpy0.423 _run_code runpy.py:64└─ 0.423 <module> sum_test.py:1├─ 0.224 <module> numpy/__init__.py:1│ [231 frames hidden] numpy, pickle, struct, <built-in>, pa...│ 0.088 create_dynamic <built-in>:0├─ 0.176 sum <built-in>:0│ [2 frames hidden] <built-in>└─ 0.024 RandomState.rand <built-in>:0[2 frames hidden] <built-in>To view this report with different options, run:pyinstrument --load-prev 2022-05-27T15-05-41 [options]
Add some more zeros to the number in big_array= np.random.rand(1000000)
and you can really see the sum()
step become the rate-limiting one in the whole process of running that code.