0

The question is very straight forward. Content here is information collected during the run of an interconnected system. I simplified functions to emphasise the bottlenecks from line_profiler into three dependend functions objectively similar to real code.

As of now it seems all lines are mandatory, but I refuse to believe this. I did try simplifying logical parts with .all() and .any() but either I am not proficient in Python, or I keep running into a bizarre limitation; just keep giving me a different result than expected.

If you are unable to gather full use case of the 3 functions, try to comprehend on a purely syntactic manner (while at least preserving same number of variables) and provide a usable solution to achieve the same functionality.

Total time: 419.632 s
File: switch_array_logic.py
Function: func_A at line 336

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   336                                           @profile
   337                                           def func_A(_switch,_loc,_prev_state=None,_cur_state=None,bulb_on=True):
   338  43048749   27529633.0      0.6      6.6      if bulb_on:
   339  43048651   43186202.0      1.0     10.3          _p_cur_state = _switch[_loc][2]
   340  43048651   42282929.0      1.0     10.1          _p_prev_state = _switch[_loc-1][2]
   341                                               else:
   342        98         80.0      0.8      0.0          _p_prev_state = _prev_state
   343        98         54.0      0.6      0.0          _p_cur_state = _cur_state
   348  43048749   38735332.0      0.9      9.2      cond_11 = (_p_cur_state >= 0)
   349  43048749   35887392.0      0.8      8.6      cond_10 = (_p_cur_state < 0)
   350  43048749   34747819.0      0.8      8.3      cond_00 = (_p_prev_state < 0)
   351  43048749   34685101.0      0.8      8.3      cond_01 = (_p_prev_state > 0)
   352                                           
   353  43048749   24860540.0      0.6      5.9      cond_x = cond_00 and cond_11
   354  43048749   24083554.0      0.6      5.7      cond_y = cond_10 and cond_01
   355  43048749   24902602.0      0.6      5.9      condition = cond_x or cond_y
   356                                           
   357  43048749   23279349.0      0.5      5.5      _brighness = 0
   358  43048749   23704315.0      0.6      5.6      if condition:
   359  21573867   18852346.0      0.9      4.5          _brighness = _p_prev_state/2
   360  43048749   22894531.0      0.5      5.5      return _brighness

Total time: 66.9593 s
File: switch_array_logic.py
Function: func_B at line 362

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   362                                           @profile
   363                                           def func_B(_switch,_pos,_loc,_brighness):
   364  43638012   66959275.0      1.5    100.0      return _switch[_pos][3] - _switch[_loc][3] - _brighness

Total time: 9.59384 s
File: switch_array_logic.py
Function: func_C at line 378

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   378                                           @profile
   379                                           def func_C(_switchs_slice,_valid,_PH_LEVEL):
   380    114012      82093.0      0.7      0.9      _idx = 0
   381    114012    2403035.0     21.1     25.0      _brighness = func_A(_switchs_slice,_idx+1)
   382    809223     583712.0      0.7      6.1      for _pos in range(1, _switchs_slice.shape[0]):
   383    808216     831149.0      1.0      8.7          if _valid and _switchs_slice[_idx][0] < _switchs_slice[_pos][0]: 
   385     57307      44704.0      0.8      0.5              return False,_pos 
   386    750909    1686519.0      2.2     17.6          elif ~_valid and _switchs_slice[_idx][1] > _switchs_slice[_pos][1]:
   388     47536      41899.0      0.9      0.4              return False,_pos
   389    703373    3320805.0      4.7     34.6          _ph_out = func_B(_switchs_slice,_pos,_idx,_brighness)
   391    703373     592597.0      0.8      6.2          if abs(_ph_out)  > _PH_LEVEL[1] :
   393      8162       6334.0      0.8      0.1              return True, _pos #Successful Case
   395      1007        990.0      1.0      0.0      return False,_switchs_slice.shape[0]

Additionally, as I am not familiar with Cython, but can someone try to provide an optimised version of this in Cython in answers really appreciate it. Not sure we will use it in the code. But I would very much like to see performance disparity.

Thanks in advance!

spramuditha
  • 357
  • 2
  • 9

0 Answers0