-1

How can I implement this ipython code in python?

[1]  %timeit sum(list(range(1000))) 

Ps: I want to do it in a single line of code. I have tried several times but failed every time.

Thanks.

forest-man
  • 27
  • 2
  • 6
sbhhdp
  • 353
  • 1
  • 3
  • 12
  • 1
    Possible duplicate of [How to use timeit module](https://stackoverflow.com/questions/8220801/how-to-use-timeit-module) – Chris Feb 24 '19 at 13:36
  • `ipython` does some hidden stuff regarding the environment and problem namespace. So it will be hard to implement something with the module that's a simple. – hpaulj Feb 24 '19 at 19:29

1 Answers1

0

This will give you the time taken (in seconds):

from timeit import timeit
timeTaken = timeit(lambda: sum(list(range(1000))), number=100000)

You can look up the timeit documentation for more options.

Alain T.
  • 40,517
  • 4
  • 31
  • 51