# Notes on Timing

4/3/2018

library(reticulate)
## Warning: package 'reticulate' was built under R version 3.4.4

# Timing

There is more than one way to determine if a given character is a lower case letter. We want to compare the times required by the various possibilities.

# Method 1

One simple way is to ask if the given character is in a string containing all of the letters of the alphabet.

import time
start_time = time.time()
x = 'a'
# Modify the size of the range to get about 1 second.
for i in range(10000000):
ans = x in 'abcdefghijklmnopqrstuvwxyz'
end_time = time.time()
print(end_time - start_time)
## 1.0520730018615723

# Method 2

Another method is to use the ord() function which provides a numerical value for each character. If our character has a value between the ordinal values of ‘a’ and ‘z’.

Do this one as an exercise

# Now for using the ord function
start_time = time.time()
x = 'a'
for i in range(10000000):
lo = ord('a')
hi = ord('z')
ans = ord(x) >= lo and ord(x) <= hi
end_time = time.time()
print(end_time - start_time)
# Second use of ord
## 5.625036716461182
start_time = time.time()
x = 'a'
for i in range(10000000):
lo = ord('a')
hi = ord('z')
ordx = ord(x)
ans = ordx >= lo and ordx <= hi
end_time = time.time()
print(end_time - start_time)
## 4.84155011177063

# Exercise

How much are these calls to get ord(‘a’) and ord(‘z’) costing us?

# Now for using the ord function
start_time = time.time()
x = 'a'
for i in range(10000000):

ans = ord(x) >= 97 and ord(x) <= 122
end_time = time.time()
print(end_time - start_time)
# Second use of ord
## 2.9012949466705322
start_time = time.time()
x = 'a'
for i in range(10000000):
ordx = ord(x)
ans = ordx >= 97 and ordx <= 122
end_time = time.time()
print(end_time - start_time)
## 2.4373362064361572