Last active
December 26, 2015 16:39
-
-
Save yasar11732/7181985 to your computer and use it in GitHub Desktop.
This file contains 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
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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Python 2.6+ daki bytearray gömülü fonksiyonu tam bu iş için. Ama sayılar byte limitleri içinde olmak zorunda.