+ 5

Which works fastest in python?

can you please tell me which code works fastest in python? a='1' b='2' print (a,"is not equal to", b) print (a+"is not equal to"+b) print ("{0} is not equal to{1}".format(a, b))

3rd Jul 2018, 12:08 PM
Sahil Danayak
Sahil Danayak - avatar
4 odpowiedzi
+ 32
not really sure why you need the speed of a print statement, but I would bet on the first one as the second and third need to do some string operations before passing a single value to print. However that guess relies on the assumption that print itself has a clever way of handling multiple arguments 😀. If you need to know for sure put each print in a loop with few thousand iterations and time the loops to get an average as single statements performance can vary depending on your system state at the moment of execution.
3rd Jul 2018, 1:29 PM
Nikolay Nachev
Nikolay Nachev - avatar
+ 7
Nikolay Nachev does python have a concept of flushing the output to stdout somewhat independently of the actual print statement as in C? Perhaps not as python is generally interpreted?
3rd Jul 2018, 1:49 PM
Sonic
Sonic - avatar
+ 5
by taking the tests, 1st-{0} is not equal to {1} 2nd-a+"is not equal to"+b 3rd-a,"is not equal to", b https://code.sololearn.com/c1X5F388kjT7/?ref=app https://code.sololearn.com/cuO6u32EpU4l/?ref=app https://code.sololearn.com/cFOguxjbZ01j/?ref=app
3rd Jul 2018, 3:29 PM
Sahil Danayak
Sahil Danayak - avatar
+ 1
Check out Python3 string templates for top preformance
3rd Jul 2018, 4:49 PM
Qwertiony
Qwertiony - avatar