Elapsed time in IRIX C
#1
Elapsed time in IRIX C
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);
}
KayBee
Octane

Trade Count: (0)
Posts: 132
Threads: 40
Joined: Feb 2020
Find Reply
02-10-2021, 04:35 PM
#2
RE: Elapsed time in IRIX C
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%
jan-jaap
SGI Collector

Trade Count: (0)
Posts: 1,048
Threads: 37
Joined: Jun 2018
Location: Netherlands
Website Find Reply
02-10-2021, 06:36 PM
#3
RE: Elapsed time in IRIX C
Ah, Yes! I will try this as soon as I get off work!

Thank you very much.

KB
KayBee
Octane

Trade Count: (0)
Posts: 132
Threads: 40
Joined: Feb 2020
Find Reply
02-10-2021, 06:54 PM
#4
RE: Elapsed time in IRIX C
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.
nuclear
O2

Trade Count: (0)
Posts: 4
Threads: 0
Joined: Apr 2021
Location: Arrakis
Website Find Reply
04-22-2021, 05:06 AM
#5
RE: Elapsed time in IRIX C
*writes down*

Continue gentlemen?

I'm the system admin of this site. Private security technician, licensed locksmith, hack of a c developer and vintage computer enthusiast. 

https://contrib.irixnet.org/raion/ -- contributions and pieces that I'm working on currently. 

https://codeberg.org/SolusRaion -- Code repos I control

Technical problems should be sent my way.
Raion
Chief IRIX Officer

Trade Count: (9)
Posts: 4,240
Threads: 533
Joined: Nov 2017
Location: Eastern Virginia
Website Find Reply
04-22-2021, 05:16 AM
#6
RE: Elapsed time in IRIX C
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).

"The early bird gets the worm, but the second mouse gets the cheese!"

SGI: Octane MXE, O2, Fuel (defunct), VW320 (defunct)
DEC: PC164, PC164SX, AXPpci
sgt_barnes
Octane

Trade Count: (0)
Posts: 101
Threads: 1
Joined: Mar 2019
Location: Germany
Find Reply
04-22-2021, 12:56 PM
#7
RE: Elapsed time in IRIX C
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?

Personaliris O2 Indigo2 R10000/IMPACT Indigo2 R10000/IMPACT Indigo2 Indy   (past: 4D70GT)
robespierre
refector peritus

Trade Count: (0)
Posts: 640
Threads: 3
Joined: Nov 2020
Location: Massholium
Find Reply
04-22-2021, 02:06 PM
#8
RE: Elapsed time in IRIX C
(04-22-2021, 02:06 PM)robespierre Wrote:  Q4. Who should give programming advice?

A4. Someone who understands how floating point arithmetic works.
nuclear
O2

Trade Count: (0)
Posts: 4
Threads: 0
Joined: Apr 2021
Location: Arrakis
Website Find Reply
04-28-2021, 07:58 AM
#9
RE: Elapsed time in IRIX C
(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.
jan-jaap
SGI Collector

Trade Count: (0)
Posts: 1,048
Threads: 37
Joined: Jun 2018
Location: Netherlands
Website Find Reply
04-28-2021, 08:51 AM


Forum Jump:


Users browsing this thread: 1 Guest(s)