The methods is so complicated.
I want to check the execution time of each statement
so that I can modify that part in order to increase speed.


Thanks in advance,

Raymond

Dani AI

Generated

A practical, low-effort way to find the real bottleneck is: start coarse, then drill down. That matches the spirit of suggestions already posted by , and , but adds a short, repeatable workflow and a few modern tools that make life easier.

Use this simple workflow:

  • Reproduce the real workload (representative input and run-time).
  • Run a low-overhead sampling profiler to find the few “hot” functions quickly.
  • Use a deterministic profiler to get exact call counts and time per function.
  • Only after that, micro-benchmark or use a line-level tool for fine-grained hotspots.

Practical commands and tools

  • Deterministic (function-level) profiling:

    python -m cProfile -o out.prof myscript.py
    # inspect with pstats or a visualizer

    See the cProfile docs:

  • Low-overhead sampling (good for production and native code):

    py-spy top -- python myscript.py
    py-spy record -o profile.svg -- python myscript.py

    py-spy homepage: py-spy

  • Microbenchmarks: use the timeit module or python -m timeit for tiny functions:
    timeit docs

Tips and gotchas

  • Profile on realistic inputs; tiny test inputs can hide hotspots.
  • I/O, blocking system calls, and database/network waits will appear as “hot” but need a different fix than CPU hotspots.
  • Sampling profilers are fast and usually good enough to point where to focus; deterministic profilers and line-level tools are heavier but useful once the function is identified.
  • Measure before and after changes and prefer algorithmic fixes (reduce complexity) over micro-optimizations.

If memory is a concern, add a memory profiler (e.g., Scalene or memory_profiler) after you’ve found CPU hotspots.

Recommended Answers

All 6 Replies

Python has module profile

Well, you could just do this as well:

import time
timevar1 = time.time()
# YOUR CODE GOES HERE
timevar2 = time.time()
print(timevar2-timevar1)

That will determine how many seconds it takes for your script to execute

The #5 method is quite easy to use, thanks

I do not have the time to try the above method, sorry, and Thanks to all of you!

No problem. Glad I could be of help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.