<s> from __future__ import print_function <EOL> import os <EOL> from time import time <EOL> import numpy as np <EOL> import tables as tb <EOL> import numexpr as ne <EOL> expr = "<STR_LIT>" <EOL> N = <NUM_LIT:10> * <NUM_LIT:1000> * <NUM_LIT:1000> <EOL> step = <NUM_LIT:100> * <NUM_LIT:1000> <EOL> dtype = np . dtype ( '<STR_LIT>' ) <EOL> CHUNKSHAPE = None <EOL> x = None <EOL> fprefix = "<STR_LIT>" <EOL> mpfnames = [ fprefix + "<STR_LIT>" , fprefix + "<STR_LIT>" ] <EOL> h5fname = "<STR_LIT>" <EOL> MB = <NUM_LIT> * <NUM_LIT> <EOL> def print_filesize ( filename , clib = None , clevel = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( filename , list ) : <EOL> filesize_bytes = <NUM_LIT:0> <EOL> for fname in filename : <EOL> filesize_bytes += os . stat ( fname ) [ <NUM_LIT:6> ] <EOL> else : <EOL> filesize_bytes = os . stat ( filename ) [ <NUM_LIT:6> ] <EOL> filesize_MB = round ( filesize_bytes / MB , <NUM_LIT:1> ) <EOL> print ( "<STR_LIT>" % ( <EOL> filesize_bytes , filesize_MB ) , end = '<STR_LIT:U+0020>' ) <EOL> if clevel > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" % ( clib , clevel ) ) <EOL> else : <EOL> print ( ) <EOL> def populate_x_numpy ( ) : <EOL> """<STR_LIT>""" <EOL> global x <EOL> x = np . linspace ( - <NUM_LIT:1> , <NUM_LIT:1> , N ) <EOL> def populate_x_memmap ( ) : <EOL> """<STR_LIT>""" <EOL> x = np . memmap ( mpfnames [ <NUM_LIT:0> ] , dtype = dtype , mode = "<STR_LIT>" , shape = ( N , ) ) <EOL> for i in range ( <NUM_LIT:0> , N , step ) : <EOL> chunk = np . linspace ( ( <NUM_LIT:2> * i - N ) / float ( N ) , <EOL> ( <NUM_LIT:2> * ( i + step ) - N ) / float ( N ) , step ) <EOL> x [ i : i + step ] = chunk <EOL> del x <EOL> def populate_x_tables ( clib , clevel ) : <EOL> """<STR_LIT>""" <EOL> f = tb . open_file ( h5fname , "<STR_LIT:w>" ) <EOL> atom = tb . Atom . from_dtype ( dtype ) <EOL> filters = tb . Filters ( complib = clib , complevel = clevel ) <EOL> x = f . create_carray ( f . root , "<STR_LIT:x>" , atom = atom , shape = ( N , ) , <EOL> filters = filters , <EOL> chunkshape = CHUNKSHAPE , <EOL> ) <EOL> for i in range ( <NUM_LIT:0> , N , step ) : <EOL> chunk = np . linspace ( ( <NUM_LIT:2> * i - N ) / float ( N ) , <EOL> ( <NUM_LIT:2> * ( i + step ) - N ) / float ( N ) , step ) <EOL> x [ i : i + step ] = chunk <EOL> f . close ( ) <EOL> def compute_numpy ( ) : <EOL> """<STR_LIT>""" <EOL> y = eval ( expr ) <EOL> def compute_numexpr ( ) : <EOL> """<STR_LIT>""" <EOL> y = ne . evaluate ( expr ) <EOL> def compute_memmap ( ) : <EOL> """<STR_LIT>""" <EOL> x = np . memmap ( mpfnames [ <NUM_LIT:0> ] , dtype = dtype , mode = '<STR_LIT:r>' , shape = ( N , ) ) <EOL> r = np . memmap ( mpfnames [ <NUM_LIT:1> ] , dtype = dtype , mode = "<STR_LIT>" , shape = ( N , ) ) <EOL> r [ : ] = eval ( expr ) <EOL> del x , r <EOL> print_filesize ( mpfnames ) <EOL> def compute_tables ( clib , clevel ) : <EOL> """<STR_LIT>""" <EOL> f = tb . open_file ( h5fname , "<STR_LIT:a>" ) <EOL> x = f . root . x <EOL> atom = tb . Atom . from_dtype ( dtype ) <EOL> filters = tb . Filters ( complib = clib , complevel = clevel ) <EOL> r = f . create_carray ( f . root , "<STR_LIT:r>" , atom = atom , shape = ( N , ) , <EOL> filters = filters , <EOL> chunkshape = CHUNKSHAPE , <EOL> ) <EOL> ex = tb . Expr ( expr ) <EOL> ex . set_output ( r ) <EOL> ex . eval ( ) <EOL> f . close ( ) <EOL> print_filesize ( h5fname , clib , clevel ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> tb . print_versions ( ) <EOL> print ( "<STR_LIT>" , <EOL> round ( <NUM_LIT:2> * N * dtype . itemsize / MB , <NUM_LIT:1> ) , "<STR_LIT>" ) <EOL> supported_clibs = [ clib for clib in ( "<STR_LIT>" , ) <EOL> if tb . which_lib_version ( clib ) ] <EOL> for what in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> print ( "<STR_LIT>" % ( what , N ) ) <EOL> t0 = time ( ) <EOL> if what == "<STR_LIT>" : <EOL> populate_x_numpy ( ) <EOL> compute = compute_numpy <EOL> elif what == "<STR_LIT>" : <EOL> populate_x_numpy ( ) <EOL> compute = compute_numexpr <EOL> elif what == "<STR_LIT>" : <EOL> populate_x_memmap ( ) <EOL> compute = compute_memmap <EOL> print ( "<STR_LIT>" , round ( time ( ) - t0 , <NUM_LIT:3> ) ) <EOL> print ( "<STR_LIT>" % ( expr , what ) ) <EOL> t0 = time ( ) <EOL> compute ( ) <EOL> print ( "<STR_LIT>" , <EOL> round ( time ( ) - t0 , <NUM_LIT:3> ) ) <EOL> for what in [ "<STR_LIT>" ] : <EOL> t0 = time ( ) <EOL> first = True <EOL> for clib in supported_clibs : <EOL> for clevel in range ( <NUM_LIT:10> ) : <EOL> if not first and clevel == <NUM_LIT:0> : <EOL> continue <EOL> print ( "<STR_LIT>" % ( what , N ) ) <EOL> populate_x_tables ( clib , clevel ) <EOL> print ( "<STR_LIT>" , round ( time ( ) - t0 , <NUM_LIT:3> ) ) <EOL> print ( "<STR_LIT>" % ( expr , what ) ) <EOL> t0 = time ( ) <EOL> compute_tables ( clib , clevel ) <EOL> print ( "<STR_LIT>" , <EOL> round ( time ( ) - t0 , <NUM_LIT:3> ) ) <EOL> first = False </s>
