def make_item_str(key, val): if explicit or kwargs.get('strkeys', False): key_str = six.text_type(key) else: key_str = repr2(key, precision=precision, newlines=0) prefix = key_str + kvsep kwargs['_return_info'] = True val_str, _leaf_info = repr2(val, **kwargs) # If the first line does not end with an open nest char # (e.g. for ndarrays), otherwise we need to worry about # residual indentation. pos = val_str.find('\n') first_line = val_str if pos == -1 else val_str[:pos] compact_brace = kwargs.get('cbr', kwargs.get('compact_brace', False)) if compact_brace or not first_line.rstrip().endswith(tuple('([{<')): rest = '' if pos == -1 else val_str[pos:] val_str = first_line.lstrip() + rest if '\n' in prefix: # Fix issue with keys that span new lines item_str = prefix + val_str else: item_str = ub.hzcat([prefix, val_str]) else: item_str = prefix + val_str return item_str, _leaf_info
def main(): import ubelt as ub dis_results = [ dis2(loop_first), ' | ', dis2(loop_first2), ] print(ub.hzcat(dis_results)) rows = benchmark_loop_first_variants() import pandas as pd df = pd.DataFrame(rows) import kwplot plt = kwplot.autoplt() # import matplotlib as mpl # mpl.use('Qt5Agg') # from matplotlib import pyplot as plt import seaborn as sns sns.set() sns.lineplot(data=df, x='num_items', y='mean', hue='method_name', markers='method_name') plt.show()
def compare_dis(): """ Look at the disasembly of the methods in question to gain insight 46 0 LOAD_CONST 1 (0) | 54 0 LOAD_CONST 1 (0) 2 STORE_FAST 1 (x) 2 STORE_FAST 1 (x) 47 4 LOAD_GLOBAL 0 (loop_first) 55 4 LOAD_GLOBAL 0 (enumerate) 6 LOAD_FAST 0 (items) 6 LOAD_FAST 0 (items) 8 CALL_FUNCTION 1 8 CALL_FUNCTION 1 10 GET_ITER 10 GET_ITER >> 12 FOR_ITER 28 (to 42) >> 12 FOR_ITER 32 (to 46) 14 UNPACK_SEQUENCE 2 14 UNPACK_SEQUENCE 2 16 STORE_FAST 2 (is_first) 16 STORE_FAST 2 (idx) 18 STORE_FAST 3 (item) 18 STORE_FAST 3 (item) 48 20 LOAD_FAST 2 (is_first) 56 20 LOAD_FAST 2 (idx) 22 POP_JUMP_IF_FALSE 32 22 LOAD_CONST 1 (0) 24 COMPARE_OP 2 (==) 49 24 LOAD_FAST 1 (x) 26 POP_JUMP_IF_FALSE 36 26 LOAD_CONST 2 (1) 28 INPLACE_ADD 57 28 LOAD_FAST 1 (x) 30 STORE_FAST 1 (x) 30 LOAD_CONST 2 (1) 32 INPLACE_ADD 50 >> 32 LOAD_FAST 1 (x) 34 STORE_FAST 1 (x) 34 LOAD_CONST 3 (2) 36 INPLACE_ADD 58 >> 36 LOAD_FAST 1 (x) 38 STORE_FAST 1 (x) 38 LOAD_CONST 3 (2) 40 JUMP_ABSOLUTE 12 40 INPLACE_ADD >> 42 LOAD_CONST 0 (None) 42 STORE_FAST 1 (x) 44 RETURN_VALUE 44 JUMP_ABSOLUTE 12 >> 46 LOAD_CONST 0 (None) 48 RETURN_VALUE """ import ubelt as ub dis_results = [ dis2(method_loop_first), ' | ', dis2(method_enumerate), ] print(ub.hzcat(dis_results))
# An odd broadcast behavior import nprime import numpy as np import ubelt as ub primes = list(nprime.generate_primes(100)) # Shapes of [X, 1, Y] and [X, Y] broadcast to [X, X, Y] a = np.array(primes[0:6]).reshape(3, 1, 2) b = np.array(primes[6:12]).reshape(3, 2) c = a * b c_str = ('c \n{!r}'.format(c)) a_str = ('a \n{!r}'.format(a)) b_str = ('b \n{!r}'.format(b)) print(ub.hzcat([c_str, ' = ', a_str, ' * ', b_str])) print('c.shape = {!r}'.format(c.shape)) print('a.shape = {!r}'.format(a.shape)) print('b.shape = {!r}'.format(b.shape)) print('\n ---- \n Look at components \n') # look at components b_comp = c / a a_comp = c / b a_comp_str = ('a_comp \n{!r}'.format(a_comp)) b_comp_str = ('b_comp \n{!r}'.format(b_comp)) print(ub.hzcat([c_str, ' = ', a_comp_str, ' * ', b_comp_str])) print('c.shape = {!r}'.format(c.shape)) print('b_comp.shape = {!r}'.format(b_comp.shape))