Analysis of the time complexity of a function written in C

I performed the longest Common Subsequence task in C. I want to compare the time taken to complete a recursive version of a solution and a version of dynamic programming. How to find the time spent on performing the LCS function in both versions for different inputs? Also, can I use SciPy to plot these values ​​on a graph and determine time complexity?

Thanks in advance,

Razor

+3
source share
3 answers

: - , . ( ) , Python. - :

x y z

, x - , y - , , z - ,

Python:

# Load these from your data sets.
sequence_lengths = ...
recursive_times  = ...
dynamic_times    = ...

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
p1 = ax.plot(sequence_lengths, recursive_times, 'r', linewidth=2)
p2 = ax.plot(sequence_lengths, dynamic_times,   'b', linewidth=2)

plt.xlabel('Sequence length')
plt.ylabel('Time')
plt.title('LCS timing')
plt.grid(True)
plt.show()
+3

- clock() time.h:

clock_t start, elapsed;

start = clock();
run_test();
elapsed = clock() - start;

printf("Elapsed clock cycles: %ld\n", (long)elapsed);

, .

+2

. , , ( ). LCS, :

#include "timer.h"

Clock clk;
char elapsed[32];

clk_start(&clk);
lcs_recursive();
clk_stop(&clk);
printf("Elapsed time (recursive): %s\n",
       clk_elapsed_us(&clk, elapsed, sizeof(elapsed)));

lcs_dynamic().

, . , , .

.

, , SciPy. , .

+2
source

Source: https://habr.com/ru/post/1775121/


All Articles