preveen-stack / nodejs

0 stars 0 forks source link

using line_profiler with python #21

Open preveen-stack opened 4 months ago

preveen-stack commented 4 months ago
# Example script to profile using line_profiler
from line_profiler import LineProfiler

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

def main():
    # Create a LineProfiler object
    profiler = LineProfiler()

    # Add functions to be profiled
    profiler.add_function(fibonacci)

    # Start profiling
    profiler.enable_by_count()

    # Code to be profiled
    result = fibonacci(30)

    # Stop profiling
    profiler.disable_by_count()

    # Print the profiling results
    profiler.print_stats()

if __name__ == "__main__":
    main()
preveen-stack commented 4 months ago
Function: fibonacci at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           def fibonacci(n):
     5   2692537  623705000.0    231.6     43.9      if n <= 1:
     6   1346269  311692000.0    231.5     22.0          return n
     7                                               else:
     8   1346268  484488000.0    359.9     34.1          return fibonacci(n-1) + fibonacci(n-2)