Common sample programs
Time consuming
The test method uses the timeit.Timer object, defines some preparation codes by setting the setup, sets the code that needs to test the running time through the stmt, and tests the performance through the timeit method (you can enter the number of runs and calculate the average)
In the actual test, you need to warm up first, that is, run the content that needs to be run once, and then if the running time is less than 1s, you need to run it multiple times to calculate the average.
https://zhuanlan.zhihu.com/p/104440866
# Set the code that needs to be tested timer = timeit.Timer(setup='import numpy as np', stmt='np. zeros((4, 4))') timer.timeit(10) # Test code that will be used later def bench_workload(workload): """Benchmark a workload workload: a method that accepts a num_repeat argument and return its total execution time """ # Need to warm up first workload(1) # warmup # Test the running time of a single time # If the word running time is greater than 1s, then run it once time = workload(1) # the time to run once if time > 1: return time # If the word running time is less than 1s, run multiple times to calculate the average # The number of repeats to measure at least 1 second num_repeats = max(int(1.0 / time), 5) return workload(num_repeats) / num_repeats print('time for np.zeros((4, 4)):', bench_workload(timer.timeit))
Function Call Test
When testing performance, the time it takes to call a function cannot be ignored.
This section uses copyto as an example to test performance and shows the performance test chart.
The abscissa in the figure below is the length of the vector that needs to be copied, and the ordinate is the data transmission efficiency.
As can be seen from the figure, the transmission speed first increases steadily and then reaches saturation. The reason for saturation is that the bandwidth is full.
import timeit import numpy as np from matplotlib import pyplot as plt from IPython import display def np_setup(n): return 'import numpy as np\\ ' \ 'x = np.random.normal(size=%d).astype("float32")\\ ' \ 'y = np.empty_like(x)\\ '% n def np_copy(n): return timeit.Timer(setup=np_setup(n), stmt='np.copyto(y, x)') sizes = 2**np.arange(5, 20, 4).astype('int') exe_times = [bench_workload(np_copy(n).timeit) for n in sizes] # Picture 1 display.set_matplotlib_formats('svg') # one float32 takes 4 bytes plt.loglog(sizes, sizes*4/exe_times/1e9) plt.xlabel('Vector length') plt.ylabel('Throughput (GB/sec)') plt.grid() # Figure II plt.loglog(sizes, exe_times) plt.xlabel('Vector length') plt.ylabel('Time (sec)') plt.grid()
Modify file encoding
python2
#coding=utf-8 import os importsys import codecs import chardet def convert(filename,out_enc="UTF-8"): try: content=codecs.open(filename,'r').read() source_encoding=chardet.detect(content)['encoding'] #if source_encoding in ["UTF-8", "UTF-8-BOM"]: #print filename + " " + source_encoding print filename + " src_encoding:" + source_encoding content=content.decode(source_encoding).encode(out_enc) codecs.open(filename,'w').write(content) print filename + " convert_to_encoding:" + out_enc except IOError as err: print("I/O error:{0}". format(err)) def explore(dir): for root,dirs,files in os.walk(dir): # Find all files for file in files: if os.path.splitext(file)[1] in ['.cpp','.c','.h','.hpp']: # If the pattern matches the pattern, you can use glob .glob simplified #print file path=os.path.join(root,file) convert(path) def main(): explore(os.getcwd()) if __name__=="__main__": main()
python3
#coding=utf-8 import os importsys import codecs import chardet def convert(filename,out_enc="utf-8"): with open(filename, 'rb + ') as f : content = f. read() encode = chardet.detect(content)['encoding'] if (encode != out_enc): try: gbk_content = content.decode(encode) uft_byte = bytes(gbk_content, encoding=out_enc) f.seek(0) f.write(uft_byte) print(filename + "\\ src_encodeing:" + encode + " convert_to_encodeing:" + out_enc) except IOError as err: print("convert " + filename + " failed") def explore(dir): for root,dirs,files in os.walk(dir): for file in files: if os.path.splitext(file)[1] in ['.cpp','.c']: #print file if file not in ["biz_ocr_charset.c", "biz_ocr_vdpr.c"]: path=os.path.join(root,file) convert(path) def main(): change_dir=os.getcwd() + "/../code/" explore(change_dir) if __name__=="__main__": main()
python glob.glob uses to match all files that meet the conditions and return them in the form of a list
python glob.glob usage
import glob list = glob.glob(*g’) # Find the full name of the file jpg ending with g print(list) result: ['dog.1012.jpg', 'dog.1013.jpg', 'dog.1014.jpg', 'dog.1015.jpg', 'dog.1016.jpg' ] file_paths_LIST = glob.glob('%s/**/*%s' % (folder path, suffix name), recursive=True) #Recursively search for files that meet the conditions in the path
1. Use nested loops to print the multiplication table
Idea analysis:
1. Print stars 2. Use nested loops to print stars 3. Replace the stars with multiplication formulas
Print stars using nested loop
row = 1 while row < 10: # Row loop 1,2,...,9 prints 9 rows # This line needs to be operated n times to define a loop # Column loop: number of operations col=1 while col <= row: # The number of each row is equal to the current number of rows # No line breaks required print('*',end='') # There will be line breaks by default, end='', no line breaks col + = 1 # number of columns + 1 # newline print() row + = 1 # number of rows + 1
Replace stars with multiplication formulas
row = 1 while row < 10:# row loop 1,2,...,9 print 9 rows # This line needs to be operated n times to define the loop # Column loop: number of operations col=1 while col <= row:# The number of each row is equal to the current number of rows # No line breaks required #column*row=data print('%d * %d = %d'%(col,row,col*row),end='\t') # There will be line breaks by default, end='\t' , print tabs col + = 1 # number of columns + 1 # newline print() row + = 1 # number of rows + 1
Reverse order multiplication table
analyze:
Just need to start the line number from 9
row = 9 while row > 0:# row loop 9,8,...,1 prints 9 rows # This line needs to be operated n times to define the loop # Column loop: number of operations col=1 while col <= row: # The number of each row is equal to the current number of rows # No line breaks required #column*row=data print('%d * %d = %d'%(col,row,col*row),end='\t') # There will be line breaks by default col + = 1 # column + 1 # newline print() row -= 1 # row -1
Jump
def jmp(target_line): target_frame = sys._getframe(3) #Need to jump old_trance = target_frame.f_trace #target_frame.f_trace_opcode = True def new_trace_hook(cur_frame, event, arg): if event == 'line' and cur_frame == target_frame: try: cur_frame.f_lineno = target_line except ValueError as e: print("jmp failed: " + str(e)) frame.f_trace = old_trance return old_trance return old_trance frame.f_trace = new_trace_hook sys.settrace(new_trace_hook)
decorator ast code modification update function
class updateFunc(ast.NodeTransformer): def __init__(self): pass def visit_BinOp(self, node): node.op = ast.Mult() return node class warpFunc(): def __init__(self, func): self.func = func self.new_func = {<!-- -->} def __call__(self, *args, **kwargs): ast_code, src_code = get_code_ast(self.func) # ast.parse inspect.getsourcecode ast_code.body[0].decorator_list = [] trs = updateFunc() newast = trs.visit(ast_code) global_vars = {<!-- -->**globals(), **locals()} local_vars = {<!-- -->} exec(compile(newast, "file_temp.py", mode='exec'), global_vars, local_vars) return local_vars[self.func.__name__](*args) def trans(func): def _deco(func, *args, **kwargs): new_func = warpFunc(func) return new_func(*args) @trans def add(a, b) c = a + b return c print(add(3, 5))