Skip to content

Instantly share code, notes, and snippets.

@yasar11732
Last active Dec 26, 2015
Embed
What would you like to do?
from __future__ import print_function, unicode_literals
import time
from random import randint
from itertools import repeat
class Timer(object):
def __init__(self, name = "Unnamed", verbose=False):
self.name = name
self.verbose = verbose
def __enter__(self):
self.start = time.time()
def __exit__(self, exc_type, exc_value, traceback):
self.end = time.time()
if self.verbose:
duration = self.end - self.start
print("{} took {} seconds".format(self.name, duration))
bytelist = [randint(32,126) for _ in repeat(None,10000000)]
"""
for i in range(1,42):
samp = bytelist[:(i*10000)]
with Timer("{} chars".format(len(samp)),True):
string = ""
for i in samp:
string += chr(i)
"""
with Timer("for loop 1",True):
chars = []
for i in bytelist:
chars.append(chr(i))
string = "".join(chars)
def looper():
chars = []
_chr = chr
_append = chars.append
for i in bytelist:
_append(_chr(i))
string = "".join(chars)
with Timer("for loop 2", True):
looper()
with Timer("map", True):
string = "".join(map(chr, bytelist))
def looper2():
_chr = chr
string = "".join(map(chr, bytelist))
with Timer("map local", True):
looper2()
@otuncelli

This comment has been minimized.

Copy link

@otuncelli otuncelli commented Oct 27, 2013

Python 2.6+ daki bytearray gömülü fonksiyonu tam bu iş için. Ama sayılar byte limitleri içinde olmak zorunda.

with Timer("bytearray", True):
    str(bytearray(bytelist))
bytearray took 0.144000053406 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment