1

I have implemented a code in Python which uses Monte Carlo simulations, but even at N=1000 it takes too much time. I have two questions:

  1. Now I'm running it on a Notebook, would running it on Pycharm on my computer improve the performance?

  2. How can I understand which part of my code is taking the most time? I would like to know for example: your code spend x% of the time on this for loop and y% in this other for loop.

Edit:

The code in particular is for evaluating odds of poker hands, so I'm simulating a lot of boards and every time evaluating which hand is better.

paolopazzo
  • 277
  • 2
  • 14
  • 2
    can you add your code? I tend to use the time module, and place calls of t1 = time.time() in various places in my code to work out how long each bit takes. I also believe there is a timeit module which can also be used to time things – Emi OB Dec 01 '21 at 09:36
  • 1
    Have you tried timing your specific places of code where you expect to be taking more time? – Ruthvik Dec 01 '21 at 09:37
  • 1
    Try [profilehooks](https://pypi.org/project/profilehooks) – mugiseyebrows Dec 01 '21 at 09:44
  • 1
    We really cannot answer the first question – your notebook runs on *some* computer and your PyCharm runs on *some* computer. Without knowing anything about either, we cannot say which one is faster or whether they are even distinct. Many Monte Carlos simulations should not be run by either Notebook or Pycharm but a bare Python executable, perhaps even via a batch system or similar. – MisterMiyagi Dec 01 '21 at 09:50
  • 1
    As for the second: What you describe is called [profiling](https://en.wikipedia.org/wiki/Profiling_(computer_programming)). There are many options available. – MisterMiyagi Dec 01 '21 at 09:51
  • 1
    [How can you profile a Python script?](https://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script) – martineau Dec 01 '21 at 10:07
  • @martineau its exactly what I was searching! If you make it like an Answer I can accept it if you want – paolopazzo Dec 01 '21 at 12:56

1 Answers1

1

There's a pair of modules for profiling code in Python's standard library — see How can you profile a Python script?

There's a also a number of third-party modules available that can give you a more fine-grained view of the execution of your code — see How do I use line_profiler (from Robert Kern)?

Haven't tried it myself, but profilehooks also looks promising and easy to use.

martineau
  • 119,623
  • 25
  • 170
  • 301