Last active
September 21, 2015 20:22
-
-
Save plq/9bde017897070dd2bb49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1000000 loops, best of 3: 0.237 usec per loop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
yield s[-chars_left:] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 loops, best of 3: 19.4 usec per loop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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