preveen-stack / nodejs

0 stars 0 forks source link

using cprofiler in python #20

Open preveen-stack opened 1 month ago

preveen-stack commented 1 month ago
# Example script to profile using cProfile
import cProfile
import pstats

def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

def main():
    # Create a cProfile object
    profiler = cProfile.Profile()

    # Start profiling
    profiler.enable()

    # Code to be profiled
    result = fibonacci(30)

    # Stop profiling
    profiler.disable()

    # Create a Stats object
    stats = pstats.Stats(profiler)

    # Sort the profiling results by cumulative time
    stats.sort_stats('cumulative')

    # Print the profiling results
    stats.print_stats()

if __name__ == "__main__":
    main()
preveen-stack commented 1 month ago
         2692538 function calls (2 primitive calls) in 0.960 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
2692537/1    0.960    0.000    0.960    0.960 /Users/preveen/prevlabs/python/ex-cprofiler.py:5(fibonacci)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}