john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

file cstringio write 1 gb fast stat size join generator timeit

import random
''.join( random.choice( string.ascii_uppercase + string.digits ) for x in range(N) )    # string of random uppercase and digits of length N

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join( random.choice(chars) for x in range(size) )

id_generator( 8, "This string will be the subset of characters" )   # could return sgerot



import timeit
print timeit.timeit( '"-".join( str(n) for n in range(100) )' , number=10000 ) , "list comprehension"     # .42
print timeit.timeit( '"-".join( [str(n) for n in xrange(100)] )' , number=10000 ) , "as above but with xrange"   # .38

print timeit.timeit( '"-".join( [str(n) for n in range(100)] )' , number=10000 ) , "list comprehension into a list"   # .38
print timeit.timeit( '"-".join( [str(n) for n in xrange(100)] )' , number=10000 ) , "as above but with xrange"   # .37

print timeit.timeit( '"-".join( map(str, range(100)) )' , number=10000 ) , "using a map"        # .20
print timeit.timeit( '"-".join( map(str, xrange(100)) )' , number=10000 ) , "as above but with xrange"        # .18

print "hex strings"
print timeit.timeit( "''.join(random.choice(string.hexdigits) for n in xrange(100))", "import random, string" , number=10000 ) , "verbose to hex"  # 1.07
print timeit.timeit( "binascii.b2a_hex( os.urandom(100) )", "import os, binascii" , number=10000 ) , "binascii to hex from usrandom" # 0.6
print timeit.timeit( "'%030x' % random.randrange(256**100)", "import random" , number=10000 ) , "randrange to hex via string format" # 0.8





# 2013-02-28 johnpfeiffer
import os
import sys
import time
from cStringIO import StringIO as cStringIO
from StringIO import StringIO

source_file_path = os.path.abspath( __file__ )
print source_file_path
source_file = open( source_file_path )
print source_file.read()
source_file.close()

print 'WARNING: generating a 1GB file in %s' % os.getcwd()

start = time.time()
string_buffer = cStringIO()
for i in range( 1024 ):  # 1MB
    for i in range( 1024 ):  # 1 KB
      string_buffer.write( 'a' )

c_result = string_buffer.getvalue()
string_buffer.close()
print 'cStringIO completed: %.3f seconds' % ( time.time() - start )
print 'KiloBytes: %d \n' % ( len( c_result ) / 1024 )


start2 = time.time()
string_buffer = StringIO()
for i in range( 1024 ):  # 1MB
    for i in range( 1024 ):  # 1 KB
        string_buffer.write( 'b' )

result = string_buffer.getvalue()
string_buffer.close()
print 'StringIO completed: %.3f seconds' % ( time.time() - start2 )
print 'KiloBytes: %d \n' % ( len( result ) / 1024 )



start3 = time.time()
file = open( 'data.txt' , 'wb' )
for i in range( 1024 ):  # 1GB
    sys.stdout.write( '.' )
    sys.stdout.flush()
    file.write( c_result )
file.close()
print 'file write completed: %.3f seconds' % ( time.time() - start3 )

metadata = os.stat( 'data.txt' )
print 'Bytes: ' , metadata.st_size
print 'KiloBytes: ' , ( metadata.st_size / 1024 )
print 'MegaBytes: ' , ( metadata.st_size / (1024 * 1024) )

  • « Password crack John the Ripper
  • concurrency threaded multiprocess requests upload »

Published

May 13, 2013

Category

python

~320 words

Tags

  • 1 9
  • cstringio 1
  • fast 1
  • file 92
  • gb 1
  • generator 3
  • join 3
  • python 180
  • size 4
  • stat 3
  • timeit 1
  • write 10