c++ - question about algorithm complexity -


I have tried to implement two methods of recursive and dynamic method and have taken two seconds. Is there no one better in my computer or something wrong in the code? Here are the methods

1 // recurring

  #include & lt; Stdio.h & gt; # Include & lt; Time.h> # Include & lt; Iostream & gt; Using std :: cout; Zero print (int n) {if (n & gt; 0) returns; Cout & lt; & Lt; N & lt; & Lt; ""; Print (n-1); } Int main () {int n = 10; Time_t start, end; Double different; Time (& amp; Beginning); Print (n); Time (& amp; nbsp;); Dif = difftime (end, start); Printf ("It took you .21f seconds", dif); Return 0; }  

2.second method

  #include & lt; Iostream & gt; # Include & lt; Stdio.h & gt; # Include & lt; Time.h> using namespace std; Zero print (int n) {if (n & gt; 0) returns; While (n> = 0) {cout & lt; & Lt; N - & lt; & Lt; Endl; }} Int main () {int n = 10; Double different; Time_t start, end; Time (& amp; Beginning); Print (n); Time (& amp; nbsp;); Dif = difftime (end, start); Printf ("It took you .21f seconds", dif); Return 0; }   

The resolution of a second timer is not suitable for measuring such rapid operations.

I suggest that you execute your code multiple times (1 million times such a thing; use a for a loop), estimate overall time and then Calculate the average value.

This way you will get more reliable results.

Just a quick note: I think that you use cout in your works. It's a bad idea: cout and most commonly I / O operations are slow in relation to other tasks, so it is likely that your function will spend to spend your maximum time rather than computing.


Comments