Performance issues in simulation with python lists
<p>I'm currently writing a simulation in python 3.4 (miniconda).
The entire simulation is quite fast but the measurement of some simulation data is bloody slow and takes about 35% of the entire simulation time. I hope I can increase the performance of the entire simulation if I could get rid of that bottleneck. I spend quite some time to figure out how to do that but unfortunately with little success. The function MeasureValues is called in every period of the simulation run.</p>

<p>If anybody has an idea how to improve the code, I would be really grateful.</p>

<p>Thank you guys.</p>

<pre><code>def MeasureValues(self, CurrentPeriod):
    if CurrentPeriod &gt; self.WarmUp:
        self.ValueOne[CurrentPeriod] = self.FixedValueOne if self.Futurevalue[CurrentPeriod + self.Reload] &gt; 0 else 0
        self.ValueTwo[CurrentPeriod] = self.VarValueTwo * self.AmountValueTwo[CurrentPeriod]
        self.ValueThree[CurrentPeriod] = self.VarValueThree  * self.AmountValueThree[CurrentPeriod]
        self.SumOfValues[CurrentPeriod] = self.ValueOne[CurrentPeriod] + self.ValueTwo[CurrentPeriod] + self.ValueThree[CurrentPeriod]
        self.TotalSumOfValues += self.SumOfValues[CurrentPeriod]

        self.HelperSumValueFour += self.ValueFour[CurrentPeriod]
        self.HelperSumValueTwo += self.AmountValueTwo[CurrentPeriod]
        self.HelperSumValueFive += self.ValueFive[CurrentPeriod]

        self.RatioOne[CurrentPeriod] = (1 - (self.HelperSumValueFour / self.HelperSumValueFive )) if self.HelperSumValueFive &gt; 0 else 1
        self.RatioTwo[CurrentPeriod] = (1 - (self.HelperSumValueTwo  / self.HelperSumValueFive )) if self.HelperSumValueFive &gt; 0 else 1
</code></pre>

<p>The code looks basic enough that there aren't any obvious gaps for optimisation without substantial restructuring (which I don't know enough about your overall architecture to suggest).</p>

<p>Try installing <a href="http://cython.org/" rel="nofollow"><code>cython</code></a> - I believe nowadays you can install it with <code>pip install cython</code> - then use it to see if you can speed up your code any.</p>

python performance list python-3.x simulation