Would like to write code in idle that I have in Jupyter notebook. It is using Timeit

2024/10/5 14:58:35

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

Answer

With 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()


Alternative using Python installed on your system with the Python Profiler tool Pyinstrument and your code as a script

(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.

https://en.xdnf.cn/q/119056.html

Related Q&A

How to pass python variable to shell command [duplicate]

This question already has answers here:How to escape os.system() calls?(10 answers)Closed 2 years ago.Is their any way that i can pass variable value of python to my unix command Note : var is a pytho…

A function inside a function Python

This is a problem asked before but I cant really understand the other explain of this kind of problem so Im here to re-write it in more details. While studying I have encountered this kind of code tha…

Accessing a parameter passed to one function in another function

I have two functions:def f1(p1=raw_input("enter data")):...do somethingdef f2(p2=raw_input("enter data")):...do something elsep1 and p2 are the same data, so I want to avoid asking …

SOLVE: AttributeError: ImmutableDenseNDimArray object has no attribute as_independent

I am quite new to python and I have been trying to plot this in a few different ways. If I try using np.vectorize, it crashes. so i wrote this code, which is giving me the error in the title: import ma…

Python memory limit [duplicate]

This question already has answers here:In-memory size of a Python structure(7 answers)Closed 10 years ago.So clearly there cannot be unlimited memory in Python. I am writing a script that creates lists…

Automating Gmail login in Python

I am writing a Python program that can login Gmail.The purpose of this program is to check whether the username/password combination exists and is correct.Since this program is to test the the username…

Reversing order in incrementing digits

I have a list of numbers, and Im trying to do the following in a way as efficient as possible.For each consecutively incrementing chunk in the list I have to reverse its order.This is my attempt so far…

why python selenium get the empty page_source?

I try to simulate the buy item operation on the link below. (Need login in first)taobao_item_linkAnd after you click the button below. img_link:The link will jump to a new link.But if I print out the p…

How do i print the repetition output using regex it prints only first match

I have task where I need to print the occurrence and count and non-occurrence and count import resequence = 1222311m = re.search(r(\d)\1+,sequence)print(m) Exptected output : (1, 1) (3, 2) (1, 3) (2…

How do I get rid of all roles of the user discord.py

I was making my mute command and I thought of getting rid of all the members role but I dont know how to get rid of all the members roles I even tried for role in member.roles:await member.remove_roles…