{"id":10780,"date":"2024-06-28T06:33:22","date_gmt":"2024-06-28T06:33:22","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=10780"},"modified":"2024-06-28T06:35:05","modified_gmt":"2024-06-28T06:35:05","slug":"graph-the-execution-time-of-a-function-in-python","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/graph-the-execution-time-of-a-function-in-python","title":{"rendered":"Graphing the Time of Computing of a Particular Function in Python"},"content":{"rendered":"<h3>Description of below code<\/h3>\n<p>Provided Python code uses the NumPy library to calculate the factorial of integers ranging from 1 to 150. It employs the timeit module to measure the execution time for each factorial computation and stores the results in a list named time_values. The code then utilizes Matplotlib to create a plot, depicting the relationship between the input integer (n) and the corresponding time taken for factorial calculation. The x-axis represents the values of n, while the y-axis represents the time taken in seconds. The resulting plot visualizes the time complexity of the factorial computation as the input size increases.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import numpy as np\r\nimport time\r\nimport matplotlib.pyplot as plt\r\nimport timeit\r\n\r\nn_values = np.arange(1, 151)\r\ntime_values = [timeit.timeit(lambda: np.math.factorial(i), number=1) for i in n_values]\r\n\r\nplt.plot(n_values, time_values)\r\nplt.xlabel('n (Factorial of n)')\r\nplt.ylabel('Time (seconds)')\r\nplt.title('Time to Calculate Factorial of n')\r\nplt.show()\r\n<\/pre>\n<p>&nbsp;<br \/>\n<img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/06\/28062814\/unnamed-3.png\" alt=\"time to calculate\" width=\"682\" height=\"548\" class=\"alignnone size-full wp-image-10781\" srcset=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/06\/28062814\/unnamed-3.png 682w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/06\/28062814\/unnamed-3-300x241.png 300w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/p>\n<p>Or if you want to just record the time for any function then you can implement below code<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nimport time\r\n# Record the start time\r\nstart_time = time.time()\r\n# Your code or function to measure execution time\r\n# ...\r\n# Record the end time\r\nend_time = time.time()\r\n# Calculate and print the elapsed time\r\nelapsed_time = end_time - start_time\r\nprint(f\"Elapsed Time: {elapsed_time} seconds\")\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Description of below code Provided Python code uses the NumPy library to calculate the factorial of integers ranging from 1 to 150. It employs the timeit module to measure the execution time for each factorial computation and stores the results in a list named time_values. The code then utilizes Matplotlib to create a plot, depicting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10782,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-10780","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10780"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=10780"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10780\/revisions"}],"predecessor-version":[{"id":10783,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10780\/revisions\/10783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/10782"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=10780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=10780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=10780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}