ML09: Measuring Running Time in Python & R

Read time: 15 minPython code on Colab: https://bit.ly/37zaRLj

Outline

Figure 1: Funny running scene in Hunter × Hunter. (Source)

(1) Two Types of “Time” [1]

1–1 Absolute Time

1–2 Relative Time

(2) Definitions of Major Terms [2]

2–1 Civil Time

2–2 Clock

2–3 Counter

2–4 CPU Time

2–5 Process Time

2–6 Real Time

2–7 System Time

2–8 Wallclock

2–9 Elapsed Time / Elapsed Real Time

(3) Measuring Execution Time in Python

3–1 time.perf_counter() VS. time.process_time()

import time as t
import numpy as np
t1 = t.process_time()
start = t.perf_counter()
for i in np.arange(0, 10**8, 1, dtype=np.int64):
k = i * i
t2 = t.process_time()
end = t.perf_counter()
spend_1 = t2 - t1
spend_2 = end - start
print(" time.process_time(): {}".format(spend_1))
print(" time.perf_counter(): {}".format(spend_2))
Figure 2: Outcome of the 10**8 use case (left: My HP laptop) (right: Google Colab)
Figure 3: Outcome of the 10**8.5 use case (left: My HP laptop) (right: Google Colab)

3–2 A Special Use Case

from time import time, perf_counter, process_time, sleep
from datetime import datetime

def foo(n=100_000):
z = 0
for i in range(n):
z+=(i%2)**2
sleep(1.0)
return z

for f in [time, perf_counter, process_time, datetime.now]:
a = f()
foo()
b = f()
diff = b-a
if f==datetime.now:
diff = diff.seconds + diff.microseconds/10**6
print(f"{f.__name__}: {diff:.4f}")
Figure 4: Outcome on Google Colab

3–3 Another Tool in Python: timeit()

def f1():
for n in range(100):
pass
def f2():
n=0
while n<100:
n+=1
if __name__ == "__main__":
import timeit
print ("Execution Time of f1 = {}".format(timeit.timeit(f1, number=100000)))
print ("Execution Time time of f2 = {}".format(timeit.timeit(f2, number=100000)))
%timeit f1()
%timeit f2()
Figure 5: Outcome on Google Colab
import timeitcode='''
def squaring():
myList=[]
for x in range(50):
myList.append(sqrt(x))'''
print("Time = {}".format(timeit.timeit(stmt=code,setup="from math import sqrt",number=10000)))
Figure 6: Outcome on Google Colab

3–4 %timeit

%timeit for _ in range(1000): True
Figure 7: Outcome on Google Colab
import torch
import timeit
## The tensors are on CPU
a = torch.rand(100, 100)
b = torch.rand(100, 100)
%timeit a.matmul(b)
print('\n')
%timeit a.matmul(b)

3–5 Conclusion

(4) Measuring Execution Time in R

4–1 An Use Case

arr = 0:10^6.5t1 <- Sys.time()
for(i in 1:1000) arr2 = arr * 2
t2 <- Sys.time()
t = t2- t1 ; t
p1 <- proc.time()
for(i in 1:1000) arr2 = arr * 2
p2 <- proc.time()
p = p2- p1 ; p
system.time(for(i in 1:1000) arr2 = arr * 2)system.time(for(i in 1:1000) arr2 = arr * 2)
Figure 9: Timers in R

4–2 Sys.time( )

4–3 proc.time( )

4–4 system.time( )

4–5 Conclusion

(5) Conclusion of Timers in Python & R

(6) Execution Environment

My HP laptop

(7) References

ML/DS using Python & R. A Taiwanese earned MBA from NCCU and BS from NTHU with MATH major & ECON minor. Email: morton.kuo.28@gmail.com