Skip to content

Instantly share code, notes, and snippets.

@plq

plq/ben.out

Last active Sep 21, 2015
Embed
What would you like to do?
1000000 loops, best of 3: 0.237 usec per loop
import math, random
def rms(s):
num_splits = float(random.randint(1, len(s)))
#print 'nums', num_splits
chars_left = len(s)
for _ in range(int(num_splits - 1)):
min_chars_in_chunk = 1
#print '\t', 'minc', min_chars_in_chunk
max_chars_in_chunk = int(math.ceil(chars_left / num_splits))
#print '\t', 'maxc', max_chars_in_chunk
num_chars = random.randint(min_chars_in_chunk, max_chars_in_chunk)
#print '\t', 'numc', num_chars
yieldval = s[-chars_left:num_chars-chars_left]
#print '\t', 'chun', yieldval
yield yieldval
chars_left-=num_chars
#print
yield s[-chars_left:]
#!/bin/sh -x
python3 -m timeit -s 'from ben import rms' 'rms("runtime")' | tee ben.out
python3 -m timeit -s 'from sen import random_multisplitter;' 'random_multisplitter("runtime")' | tee sen.out
10 loops, best of 3: 19.4 usec per loop
import random
def random_int(s):
return random.randint(1,len(s))
def random_multisplitter(word):
from numpy import mod
spw = []
length = len(word)
rand = random_int(word)
if rand == length: #probability of not splitting
return [word]
else:
div = mod(rand, (length + 1)) #defining division points
bound = length - div
spw.append(div)
while div != 0:
rand = random_int(word)
div = mod(rand,(bound+1))
bound = bound-div
spw.append(div)
result = spw
b = 0
points =[]
for x in range(len(result)-1): #calculating splitting points
b=b+result[x]
points.append(b)
xy=0
t=[]
for i in points:
t.append(word[xy:i])
xy=i
if word[xy:len(word)]!='':
t.append(word[xy:len(word)])
if type(t)!=list:
return [t]
return t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.