Skip to content

Instantly share code, notes, and snippets.

@yuce
Created June 1, 2021 19:19
Show Gist options
  • Save yuce/cadd9b39a06bee679b75454004d520aa to your computer and use it in GitHub Desktop.
Save yuce/cadd9b39a06bee679b75454004d520aa to your computer and use it in GitHub Desktop.
import time
dv = {
1: 2,
2: 3,
3: 4,
4: 5,
5: 6,
6: 7,
7: 8,
8: 9,
9: 10,
10: 11,
11: 12,
12: 13,
13: 14,
14: 15,
15: 16,
16: 17,
17: 18,
18: 19,
19: 20,
}
def d1(x):
v = dv.get(x)
if v is None:
return 0
return v
def d2(x):
if x == 1:
return 2
elif x == 2:
return 3
elif x == 4:
return 5
elif x == 5:
return 6
elif x == 6:
return 7
elif x == 7:
return 8
elif x == 8:
return 9
elif x == 9:
return 10
elif x == 10:
return 11
elif x == 11:
return 12
elif x == 12:
return 13
elif x == 13:
return 14
elif x == 14:
return 15
elif x == 15:
return 16
elif x == 16:
return 17
elif x == 17:
return 18
elif x == 18:
return 19
elif x == 19:
return 20
return 0
def measure(f, times):
c = 0
tic = time.time()
for i in range(times):
c += f(i)
toc = time.time()
return (toc - tic)*1000
def main():
n = 100_000
d1_took = measure(d1, n)
d2_took = measure(d2, n)
print(f"d1: {d1_took}")
print(f"d2: {d2_took}")
if __name__ == '__main__':
main()
@mdumandag
Copy link

import time
import struct
from datetime import date

buf = bytearray(12)
struct.pack_into("<i", buf, 0, 1)
struct.pack_into("<i", buf, 4, 1)
struct.pack_into("<i", buf, 8, 1)


def decode():
    year = struct.unpack_from("<i", buf, 0)[0]
    month = struct.unpack_from("<i", buf, 4)[0]
    day = struct.unpack_from("<i", buf, 8)[0]
    return date(year, month, day)


dv = {
    1: 2,
    2: 3,
    3: 4,
    4: 5,
    5: 6,
    6: 7,
    7: 8,
    8: 9,
    9: 10,
    10: decode,
    11: 12,
    12: 13,
    13: 14,
    14: 15,
    15: 16,
    16: 17,
    17: 18,
    18: 19,
    19: 20,
}


def d1(x):
    v = dv.get(x)
    if v is None:
        return 0
    return v()


def d2(x):
    if x == 1:
        return 2
    elif x == 2:
        return 3
    elif x == 4:
        return 5
    elif x == 5:
        return 6
    elif x == 6:
        return 7
    elif x == 7:
        return 8
    elif x == 8:
        return 9
    elif x == 9:
        return 10
    elif x == 10:
        # no function call overhead required with
        # this approach
        year = struct.unpack_from("<i", buf, 0)[0]
        month = struct.unpack_from("<i", buf, 4)[0]
        day = struct.unpack_from("<i", buf, 8)[0]
        return date(year, month, day)
    elif x == 11:
        return 12
    elif x == 12:
        return 13
    elif x == 13:
        return 14
    elif x == 14:
        return 15
    elif x == 15:
        return 16
    elif x == 16:
        return 17
    elif x == 17:
        return 18
    elif x == 18:
        return 19
    elif x == 19:
        return 20
    return 0


def measure(f, times):
    tic = time.time()
    for i in range(times):
        f(10)
    toc = time.time()
    return (toc - tic)*1000


def main():
    n = 100_000
    d1_took = measure(d1, n)
    d2_took = measure(d2, n)
    print(f"d1: {d1_took}")
    print(f"d2: {d2_took}")


if __name__ == '__main__':
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment