Skip to content

Instantly share code, notes, and snippets.

@dbramucci
Last active February 6, 2020 08:06
Show Gist options
  • Save dbramucci/9af12bba7f644806235c6f012f2caa96 to your computer and use it in GitHub Desktop.
Save dbramucci/9af12bba7f644806235c6f012f2caa96 to your computer and use it in GitHub Desktop.
Code to run benchmarks
from typing import List
from pyrsistent import pvector
from pyrsistent.typing import PVector
def benchmark_jump(n: int):
string = 'xx.' * n
for i in range(0, n, 3):
string = jump(string, i, 1)
def benchmark_jump_sub(n: int):
string = 'xx.' * n
for i in range(0, n, 3):
string = jump_sub(string, i, 1)
def benchmark_jump_sub2(n: int):
string = 'xx.' * n
for i in range(0, n, 3):
string = jump_sub2(string, i, 1)
def benchmark_jump_p1(n: int):
string = pvector('xx.') * n
for i in range(0, n, 3):
string = jump_p1(string, i, 1)
def benchmark_jump_p2(n: int):
string = pvector('xx.') * n
for i in range(0, n, 3):
string = jump_p2(string, i, 1)
def benchmark_jump_p3(n: int):
string = pvector('xx.') * n
for i in range(0, n, 3):
string = jump_p3(string, i, 1)
def benchmark_jump_p4(n: int):
string = pvector('xx.') * n
for i in range(0, n, 3):
string = jump_p4(string, i, 1)
def benchmark_jump_mut(n: int):
string = list('xx.' * n)
for i in range(0, n, 3):
jump_mut(string, i, 1)
def jump(s: str, i: int, d: int) -> str:
assert(s[i] == 'x')
assert(s[i+d] == 'x')
assert(s[i + 2*d] == '.')
ls = list(s)
ls[i] = '.'
ls[i+d] = '.'
ls[i+2*d] = 'x'
return(''.join(ls))
def jump_p1(s: PVector[str], i: int, d: int) -> PVector[str]:
assert(s[i] == 'x')
assert(s[i+d] == 'x')
assert(s[i + 2*d] == '.')
s2 = s.set(i, '.')
s3 = s2.set(i+d, '.')
s4 = s3.set(i+2*d, 'x')
return s4
def jump_p2(s: PVector[str], i: int, d: int) -> PVector[str]:
assert(s[i] == 'x')
assert(s[i+d] == 'x')
assert(s[i + 2*d] == '.')
s = s.set(i, '.')
s = s.set(i+d, '.')
s = s.set(i+2*d, 'x')
return s
def jump_p3(s: PVector[str], i: int, d: int) -> PVector[str]:
assert(s[i] == 'x')
assert(s[i+d] == 'x')
assert(s[i + 2*d] == '.')
s_e = s.evolver() # makes a mutable interface that doesn't actually change s
s_e[i] = '.'
s_e[i+d] = '.'
s_e[i+2*d] = 'x'
# Need to freeze our evolver and get back a PVector
s_new = s_e.persistent() # Does all 3 changes with 1 update to s
return s_new
def jump_p4(s: PVector[str], i: int, d: int) -> PVector[str]:
assert(s[i] == 'x')
assert(s[i+d] == 'x')
assert(s[i + 2*d] == '.')
return s.mset(i, '.', i+d, '.', i+2*d, 'x')
def jump_sub(s: str, i: int, d: int) -> str:
assert(s[i] == 'x')
assert(s[i+d] == 'x')
assert(s[i + 2*d] == '.')
s = s[:i] + '.' + s[i+1:]
s = s[:i+d] + '.' + s[i+d+1:]
return s[:i+2*d] + 'x' + s[i+2*d+1:]
def jump_sub2(s: str, i: int, d: int) -> str:
assert(s[i] == 'x')
assert(s[i+d] == 'x')
assert(s[i + 2*d] == '.')
return (s[:i] + '.'
+ s[i+1:i+d] + '.'
+ s[i+d+1:i+2*d] + 'x'
+ s[i+2*d+1:])
def jump_mut(s: List[str], i: int, d: int):
assert(s[i] == 'x')
assert(s[i+d] == 'x')
assert(s[i + 2*d] == '.')
s[i] = '.'
s[i+d] = '.'
s[i+2*d] = 'x'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment