IRIX Network Forums
Elapsed time in IRIX C - Printable Version

+- IRIX Network Forums (//forums.irixnet.org)
+-- Forum: SGI/MIPS (//forums.irixnet.org/forum-3.html)
+--- Forum: Development/Porting (//forums.irixnet.org/forum-9.html)
+--- Thread: Elapsed time in IRIX C (/thread-2705.html)



Elapsed time in IRIX C - KayBee - 02-10-2021

Hi Again All,

I find many examples such as this online for checking the amount of elapsed time. Does anyone have a functioning example in C on SGI?

Thank you.

KB

Code:
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main() {

  struct timeval  tv;
  gettimeofday(&tv, NULL);

double begin =
  (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;


sleep(2);

gettimeofday(&tv, NULL);

double end =
  (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;

  printf("Execution time %f\n", end - begin);
  return (0);
}



RE: Elapsed time in IRIX C - jan-jaap - 02-10-2021

Code:
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>

int main(int argc, char **argv)
{
  struct timeval  tv;
  double begin, end;

  gettimeofday(&tv, NULL);
  begin =  (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;

  sleep(2);

  gettimeofday(&tv, NULL);
  end = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;

  printf("Execution time %f ms\n", end - begin);

  return 0;
}


Code:
mgras 7% uname -a
IRIX mgras 5.3 12201932 IP22 mips
mgras 8% cc -fullwarn -o t t.c
mgras 9% ./t                 
Execution time 2003.000000 ms
mgras 10%



RE: Elapsed time in IRIX C - KayBee - 02-10-2021

Ah, Yes! I will try this as soon as I get off work!

Thank you very much.

KB


RE: Elapsed time in IRIX C - nuclear - 04-22-2021

It's an extremely bad idea to use floating point numbers for this. Subtracting two huge floating point numbers will lead to large precision errors. It's ok to convert the result to a double at the end to print it easily, but do the subtraction with longs.


RE: Elapsed time in IRIX C - Raion - 04-22-2021

*writes down*

Continue gentlemen?


RE: Elapsed time in IRIX C - sgt_barnes - 04-22-2021

Oh, yeah! I'm completely with nuclear!

And keep in mind that it is also a bad idea to do any floatingpoint operations with very large and very small (as in close to zero) values. Like calculating the ETA of your next frame by adding the microsecond frame duration to the current real time. This will probably do nothing at all.

Do all time calulations with longs and in microseconds, and only ever convert to double when displaying. It will save you a lot of headache in the long term.

O.T.: The best floatingpoint explanation I ever saw comes from Fabien Sanglard: Floating Point Visually Explained (<- that's a link, btw).


RE: Elapsed time in IRIX C - robespierre - 04-22-2021

When my eyes roll back hard enough I sometimes go Socratic.

Q1. How many bits of significand precision are in the IEEE-754 DOUBLE format?

Q2. How many bits of precision are in a "long" (doubleword)?

Q3. What is the practical difference between A1 and A2?

Q4. Who should give programming advice?


RE: Elapsed time in IRIX C - nuclear - 04-28-2021

(04-22-2021, 02:06 PM)robespierre Wrote:  Q4. Who should give programming advice?

A4. Someone who understands how floating point arithmetic works.


RE: Elapsed time in IRIX C - jan-jaap - 04-28-2021

(04-22-2021, 05:06 AM)nuclear Wrote:  It's an extremely bad idea to use floating point numbers for this. Subtracting two huge floating point numbers will lead to large precision errors. It's ok to convert the result to a double at the end to print it easily, but do the subtraction with longs.

While I completely agree with you the question was "why doesn't this work", not "please rewrite this so I get a good grade for my programming assignment". The reason it didn't compile was that not all variables were declared before the first statement.