0

I want to calculate the running time of a function for my assignment. I keep getting 0 nut my teacher strictly said that he want some non zero value even for a loop of 10 size. My code so far is this

#include <chrono>

using namespace std;
using std::chrono::duration_cast;
using std::chrono::nanoseconds;
using std::chrono::steady_clock;

steady_clock::time_point t1 = steady_clock::now();
for(int i=0;i<times;i++){
    bubbleSort(arr,size);
}
steady_clock::time_point t2 = steady_clock::now();
cout<<(float)duration_cast<nanoseconds>(t2-t1).count()/times<<"     ";

i have also tried chrono::high_resolution_clock , clock() and many others. The main problem is that i have to give some non zero value for time=1; and size=10. Please suggest how could i do this?

SSMA
  • 497
  • 1
  • 7
  • 19

1 Answers1

0

I tried running your code, commenting the bubbleSort part of course. It is giving some non zero value. Try this :

cout<<(float)duration_cast<nanoseconds>(t2-t1).count()/(float)times<<"     ";
Wajahat
  • 1,593
  • 3
  • 20
  • 47
  • @SSMA What compiler are you using? Is it Visual Studio? – David G Feb 05 '14 at 21:03
  • Yes Visual Studio 2012 – SSMA Feb 05 '14 at 21:05
  • 1
    @SSMA , try running your code at http://www.compileonline.com/compile_cpp11_online.php – Wajahat Feb 05 '14 at 21:05
  • @SSMA Visual Studio has problems with the C++11 date and time facilitates. There was a question posted about a year ago from someone with the same problem but I can't find it at the moment. – David G Feb 05 '14 at 21:07
  • @Wajahat thanks for your suggestion. It worked on compileonline.com. But is there a way to make it work on visual studio? – SSMA Feb 05 '14 at 21:32
  • Why do you really need to run it on visual studio, as @0x499602D2 said there are problems with it. – Wajahat Feb 05 '14 at 21:35
  • @SSMA Repeating what Wajahat asked, why do you need to run it on Visual Studio? – David G Feb 05 '14 at 21:50
  • @Wajahat I have to submit an exe file not the code. I can only create the exe through visual studio. – SSMA Feb 06 '14 at 11:07
  • @0x499602D2 I have to submit an exe file not the code. I can only create the exe through visual studio. – SSMA Feb 06 '14 at 11:08
  • I think you can create an exe in other ways also, compilation actually converts it into an executable. Here is a link: http://stackoverflow.com/questions/8450121/how-do-i-create-a-exe-from-a-cpp-file-in-code-blocks – Wajahat Feb 06 '14 at 11:26