A simple loop is faster in python, adding types makes it 4-8 times slower #137
-
Take the following mojo block: from Time import now
mojo_t1 = now()
for i in range(10000):
pass
mojo_t2 = now()
print(mojo_t2 - mojo_t1) I get between 250 to 320 nanoseconds. Same in Python doesn't go beyond 260 nanoseconds. What is more interesting is re-writing it to: from Time import now
let mojo_t1: Int = now()
for i in range(10000):
pass
let mojo_t2: Int = now()
print(mojo_t2 - mojo_t1) take the execution time to 1550-2150. What is going on here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The execution time you are measuring is entirely the notebook backend executing the code line-by-line. The loop doesn't execute at all -- it's optimized away since the body is empty. The time you are measuring is likely noise. In the first snippet, the type of |
Beta Was this translation helpful? Give feedback.
The execution time you are measuring is entirely the notebook backend executing the code line-by-line. The loop doesn't execute at all -- it's optimized away since the body is empty. The time you are measuring is likely noise. In the first snippet, the type of
mojo_t1
is already going to beInt
, since it is inferred from the result type ofnow()
.