O 表示法中的运行时间代码
我想知道如何计算 C++ 程序中 O_notation 的运行时间?有相关代码吗?
我必须使用这段代码来显示运行时间
clock_t start, end;
start = clock();
//CODES GOES HERE
end = clock();
std::cout << end - start << "\n";
std::cout << (double) (end-start) / CLOCKS_PER_SEC;
,但我想用 O_notation 代码计算它,以便在 2 个程序 min-heap 和带有数组的 prim 算法中实现它。
I want to know how can I calculate running time in O_notation in C++ programs? Is there any code for that?
I have to use this code for showing the running time
clock_t start, end;
start = clock();
//CODES GOES HERE
end = clock();
std::cout << end - start << "\n";
std::cout << (double) (end-start) / CLOCKS_PER_SEC;
But I want to calculated it in O_notation code to implement it in 2 programs min-heap and prim's algorithm with array.
假设您有明确定义的输入和输出格式,则您有合理的机会针对各种大小的输入运行相关代码,并对各种大小所需的时间进行(例如)多项式曲线拟合。
例如,您将针对 10、100、1000 和 10000 个输入运行代码。如果每次更改运行时间大约延长 10 倍,则您似乎拥有线性算法。如果每次的长度大约是原来的 100 倍,则看起来是二次的,依此类推。
Assuming you have well-defined formats for input and output, you stand a reasonable chance of running the code in question for various sizes of input, and doing (for example) a polynomial curve fit to the times take for the various sizes.
So, for example, you'd run the code for 10, 100, 1000, and 10000 inputs. If the run-time gets roughly 10 times as long with each change, you appear to have a linear algorithm. If it gets roughly 100 times as long each time, you appear to have a quadratic one, and so on.