-1

The following program prints out duration=0.000000000000000000 for some reason. I've read a very similar question The C `clock()` function just returns a zero , but suggestions there didn't help. The loop between which i take time samples is of 100 iterations, and for each one there is a Newton's root finding method with 0.00000001 precision going on, surely it can't take 0 seconds^-20 to compute? Sorry if this questions looks really homework-ish, I'm really noob when it comes to coding. Anybody sees what might be wrong here? Thanks!

double f(...){...}
double h;
double newton (double prev_step_solution, double x)
{
  double root=prev_step_solution;
  while (((prev_step_solution+h*f(root,x)-root)>0.00000001)
    ||((prev_step_solution+h*f(root,x)-root)<-0.00000001))
  {
    root = root-(h*f(root,x)+prev_step_solution-root)/(h*fder(root,x)-1);
  }
  return root;
}             

int main()
{
  int i;
  double euler [2][101];
  h=1/100;
  std::clock_t start;   
  double duration;

  start = std::clock();
  for (i=1; i<=200; i++)
  {
    euler [0][i] = h*i;
    euler [1][i] = newton(euler[1][i-1],h*(i-1));
  }
  duration = ((double)( std::clock()-start  ))/((double) CLOCKS_PER_SEC);

  printf(" euler %.20f \n",(double)duration);
  return 0;
}
Philip Nelson
  • 1,027
  • 12
  • 28
  • 3
    Try actually printing the results. If you don't use them, the compiler is free to optimize them away causing 0 time to be used. – NathanOliver Oct 31 '19 at 13:29
  • 1
    C and C++ are different languages with different programming techniques and idioms. Please do not tag both. – L. F. Oct 31 '19 at 13:31
  • Those numbers are correct. Yes, C++ really is _that_ fast. – nada Oct 31 '19 at 13:39
  • Also note that your program has undefined behavior right from the start. You never initialized `euler[1][0]` when you use it in the first iteration and later you iterate out-of-bounds because the array has only 101 elements, not 201. – walnut Oct 31 '19 at 14:02

2 Answers2

0

Works just fine for me. There are errors in your program though:

for (i=1;i<=200;i++)
{
    euler[0][i]=h*i;
    euler[1][i]=newton(euler[1][i-1],h*(i-1));
}

The size of the euler array is euler[2][101] (101) but you are iterating through it 200 times.

Change the loop to for (i=1;i<=100;i++)

Also, initialize start -> std::clock_t start = std::clock();

AmN
  • 331
  • 1
  • 7
  • "*Works just fine for me.*" should be a comment and I think that mentioning the out-of-bounds should also be a comment, as it is unlikely to be primary cause of the problem. Did you test the code with optimizations enabled? I could not because the code is incomplete, but I expect the whole calculation to be optimized out as @NathanOliver suggested in the comments. – walnut Oct 31 '19 at 13:58
  • Even if I use just this: `for (int i = 1; i <= 100; i++) euler[0][0] = 1;` and enable O3 optimizations `clang++ main.cpp --std=c++17 -O3 && ./a.out` I get: `euler 0.00000100000000000000`. It does get optimized out, but still the program should take some CPU time to finish. -O3 or even -O1 remove the for loop. – AmN Oct 31 '19 at 14:17
  • You are measuring only the time it needs to fetch the instructions for `std::clock`. If you add a warm-up call for it, then you can get it down to exactly zero easily, see [compiler explorer](https://godbolt.org/z/hrdfrK). (If the values shown are not zero, click the recompile button a few times, it will vary occasionally.) Also note that there nothing except a `mov` for the return value in between the `std::clock` calls in the assembly. – walnut Oct 31 '19 at 14:29
0

A different approach to timing is to use the std::chrono library.

#include <chrono>
#include <iostream>

int main()
{
  auto start = std::chrono::steady_clock::now();

  // do work

  auto end = std::chrono::steady_clock::now();

  auto result = std::chrono::duration<double, std::milli>(end - start).count();

  std::cout << result << " milliseconds\n";

}
Philip Nelson
  • 1,027
  • 12
  • 28
  • 1
    Note that this measures wall clock, not processor time as `std::clock` does. I am also unsure whether you expect this approach to yield a different result and if so why? – walnut Oct 31 '19 at 14:05
  • Honestly I was unaware of the differences and unaware of `std::clock` all together. Thanks for teaching me. – Philip Nelson Oct 31 '19 at 14:33