Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python – time.time() vs. bash time

I've been working on some Project Euler problems in Python 3 [osx 10.9], and I like to know how long they take to run.

I've been using the following two approaches to time my programs:

1)

import time
start = time.time()

[program]

print(time.time() - start)

2) On the bash command line, typing time python3 ./program.py

However, these two methods often give wildy different results. In the program I am working on now, the first returns 0.000263 (seconds, truncated) while the second gives

real    0m0.044s
user    0m0.032s
sys     0m0.009s

Clearly there is a huge discrepancy - two orders of magnitude compared to the real time.

My questions are:
a) Why the difference? Is it overhead from the interpreter?
b) Which one should I be using to accurately determine how long the program takes to run? Is time.time() accurate at such small intervals?

I realize these miniscule times are not of the utmost importance; this was more of a curiosity.

Thanks.

[UPDATE:]
Thank-you to all of the answers & comments. You were correct with the overhead. This program:

 import time
 start = time.time()

 print("hello world")

 print(time.time() - start)

takes ~0.045 sec, according to bash.

My complicated Project Euler problem took ~0.045 sec, according to bash. Problem solved.

I'll take a look at timeit. Thanks.

like image 525
baum Avatar asked Jan 02 '14 21:01

baum


People also ask

Is Bash or Python faster?

Python is faster than Bash and is ranked 1st, while Bash is ranked 34th. The most important reasons people chose Python are that it can be used for almost any task. It works on most major operating systems and is also installed by default on most Unix/Linus systems. It is very similar to writing pseudocode.

Which is better Bash or Python?

Bash is a software replacement for the original Bourne shell. Python is easy, simple and powerful language. Bash is tough to write and not powerful as python. It is specially designed for web and app development.

Is bash the same as Python?

No. Python is a programming language mostly used in automation programming. Bash is a command-line interpreter or user shell to interpret user commands. Python is developed as an easy to implement an object-oriented programming language.

How do you time a bash script?

Using Bash Shell's TIMEFORMAT The TIMEFORMAT is a string format that will be printed after the execution of the block code inside the time{} wrapper finishes. The %R specifies to print the elapsed time in seconds with milliseconds precision. Let's test our script: $ ./elapsed_time.sh It took 12.008 seconds.


2 Answers

The interpreter imports site.py and can touch upon various other files on start-up. This all takes time before your import time line is ever executed:

$ touch empty.py
$ time python3 empty.py 

real    0m0.158s
user    0m0.033s
sys     0m0.021s

When timing code, take into account that other processes, disk flushes and hardware interrupts all take time too and influence your timings.

Use timeit.default_timer() to get the most accurate timer for your platform, but preferably use the timeit module itself to time individual snippets of code to eliminate as many variables as possible.

like image 80
Martijn Pieters Avatar answered Sep 21 '22 19:09

Martijn Pieters


Because when you run the time builtin in bash the real time taken includes the time taken to start up the Python interpreter and import the required modules to run your code, rather than just timing the execution of a single function in your code.

To see this, try for example

import os
import time
start = time.time()
os.system('python <path_to_your_script>')
print time.time() - start

You'll find that this is much closer to what time reports.

like image 35
Iguananaut Avatar answered Sep 25 '22 19:09

Iguananaut