0

I wrote this code and try to learn a bit more how to code more efficiently and increase performance.

import random

def CalcAverageSolarFlareEvent(EventList):
    return sum(EventList) / len(EventList)

percentage_solar_flare = 12
decade_counting = 0
Event = []
CurrentYear = 2022

for Simulations in range(1, 999999):
    while True:
        if random.randint(1, 100) != percentage_solar_flare:
            decade_counting += 1
        else:
            Event.append(decade_counting)
            decade_counting = 0
            break

print("In the Year "+str(int(CalcAverageSolarFlareEvent(Event))*10+CurrentYear) +
      " we got a Solarflare")

I tried to calculate the decade_counting and adding current year at the end, to give more ram.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • This looks more like a codereview question – Chris Jun 28 '22 at 17:18
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Dejene T. Jun 28 '22 at 17:22
  • Sorry, my problem is it took too long and I try to make it a bit faster. I know, hardware can solve this problem but i want to know how i can meke it with code first. – Yohenis Cruz Salazar Jun 28 '22 at 17:25
  • 1
    So did you profile the script? Do you know what parts need optimization and which don't? If you don't know either of those things, then you didn't do enough research on your own before posting here. And besides, like was said already, if this code works, then StackOverflow isn't even necessarily the correct site to ask this on, since you're seemingly mainly asking for a code review. – Random Davis Jun 28 '22 at 17:33
  • See [How do I profile a Python script?](https://stackoverflow.com/questions/582336/how-do-i-profile-a-python-script) – martineau Jun 28 '22 at 17:54

1 Answers1

0

Python is not great for such a code, especially the standard CPython implementation. Consider using PyPy or Pyston or an embedded JIT (just-in-time compiler) like Numba, or alternatively a compiled language.

Moreover, you do not need to add items to a list so to count them or sum them: you can compute a partial sum on the fly.

Here is a modified code using the Numba JIT:

import random
import numba as nb

@nb.njit('()')
def compute():
    percentage_solar_flare = 12
    decade_counting = 0
    sum_Events = 0
    count_Event = 0
    CurrentYear = 2022

    for Simulations in range(1, 999_999):
        while True:
            if random.randint(1, 100) != percentage_solar_flare:
                decade_counting += 1
            else:
                sum_Events += decade_counting
                count_Event += 1
                decade_counting = 0
                break

    print("In the Year "+str(int(sum_Events/count_Event)*10+CurrentYear) +
          " we got a Solarflare")

compute()

The initial code takes 65 second on my machine while this one takes 4 seconds. Thus, it is about 16 times faster. Most of the time is spent in generating random values. SIMD instructions and multithreading can help to improve performance further (by 1~2 order of magnitude) but this is certainly not a good idea to use them is you are a beginner.

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59