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.

7
  • 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 Commented Dec 1, 2021 at 9:36
  • 1
    Have you tried timing your specific places of code where you expect to be taking more time? Commented Dec 1, 2021 at 9:37
  • 1
    Try profilehooks Commented Dec 1, 2021 at 9: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. Commented Dec 1, 2021 at 9:50
  • 1
    As for the second: What you describe is called profiling. There are many options available. Commented Dec 1, 2021 at 9:51

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.