Hi, I want to calculate the execution time and the number of cycles for the small set of code (python code). Currently, I am working with Raspberry Pi 3B (ARM Cortex-A53 SoC processor) and writing a python code. Now, I have to calculate execution time as well as a number of cycles for a small set of code. For example:
a = 10
b = 10
print("Sum ", (a+b))
IMNSHO, this is nearly impossible. (for example, it'd depend on which version of Python you're using, and print() speed would also depend on which version of an operating system you are running and where you are printing to, and whether you want to include the time that it takes the characters to actually appear before the user, or just how long it takes the output to go into some operating system buffer where it will be "eventually displayed for the user.")
(In any case, "print" is likely to be far, far slower than the assignments and the addition.)
If, it is impossible, then how can you say that print() is very slow. Is there any approach or calculation
.
"unpredictable" doesn't mean "unmeasurable." Any call to a "real" operating system (like Linux) has a lot of overhead just in switch context back and forth between the user and the operating system. (even on a deeply embedded system, the cost of an "SVC" instruction (causing exception-like handling) is perhaps 5x more than a "BL" based function call. Then you have to decode what function is being asked for and ... more. Adding an real OS adds additional context including caches and pager tables, and who knows what else.)
If you take a python program like:
do_io = Falsefor i in range(0, 1000000): a = 10 b = 10 if (do_io): print("Sum ", (a+b)) else: c = a + b
do_io = False
for i in range(0, 1000000):
if (do_io):
else:
c = a + b
and run it with "time python3 foo.py > /dev/null", you get times about 5x higher for the do_io=True version. (and in that case, they're both having to load and initialize the python interpreter as well, which is no small task.)