LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2009, 01:17 PM   #16
Biddle
Member
 
Registered: Jan 2009
Posts: 37

Rep: Reputation: 17

Well I suppose the question is:
How do you suppose we represent a negative number in an unsigned int?
In reflection both codes return a value which indicates if the value is positive or negative, which I failed to notice in your code sample.
I stand corrected.
 
Old 03-17-2009, 01:33 PM   #17
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, waqasdaar -

Now that you've got these cool routines (and they *are* cool!) ... please be sure to take the results with a pound of salt (or better, to quote Andrew Tanenbaum, with a "metric ton of salt").

Results are seldom accurate to-the-millisecond, much less to the microsecond. At the very least, you'll definitely want to run multiple tests, and average your results...

Last edited by paulsm4; 03-17-2009 at 01:45 PM.
 
Old 03-17-2009, 01:45 PM   #18
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by paulsm4 View Post
Now that you've got these cool routines (and they *are* cool!) ... please be sure to take the results with a pound of salt (or better, to quote Andrew Tanenbaum, with a "metric ton of salt").
Your, um, timing, sir, is impeccable.
 
Old 03-17-2009, 02:24 PM   #19
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I don't understand why the other functions posted are so complicated. It can be done in a single long line.
Code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/timex.h>


static int difference_micro(struct ntptimeval *bBefore,
struct ntptimeval *aAfter)
{
	return (signed long long) aAfter->time.tv_sec * 1000000ll +
	       (signed long long) aAfter->time.tv_usec -
	       (signed long long) bBefore->time.tv_sec * 1000000ll -
	       (signed long long) bBefore->time.tv_usec;
}

int main()
{
	srand(time(NULL));
	struct ntptimeval start, elapsed;

	struct timespec timeout = { rand() % 2, rand() % 1000000000 };

	ntp_gettime(&start);
	nanosleep(&timeout, NULL);
	ntp_gettime(&elapsed);

	fprintf(stdout, "{ %u, %.9u } -> %ims\n", timeout.tv_sec, timeout.tv_nsec,
	  difference_micro(&start, &elapsed) / 1000);
}
Kevin Barry
 
Old 03-17-2009, 04:02 PM   #20
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by ta0kira View Post
I don't understand why the other functions posted are so complicated. It can be done in a single long line.
You're right, it can. But the reason the other functions are that way is not because waqasdaar needed the extra complication (he didn't), but because of the history of the thread. jiml8 posted something that worked for him, which returned the difference as a struct timeval. I changed it somewhat so it returned both a struct timeval and a long long.

The code kinda changed incrementally. Or, as one of my past cow-orkers was known to say, "excrementally".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Current time in milliseconds charubcl Linux - Newbie 2 08-08-2008 05:28 AM
Time in milliseconds davidguygc Programming 2 04-25-2007 06:03 PM
calculate time difference between 2 formatted timestamps nickleus Linux - General 3 05-12-2006 10:08 PM
C/C++ API for retrieving system time in micro or milliseconds syseeker Programming 1 05-04-2006 07:15 AM
calculate system clock in milliseconds bndpatel Linux - General 1 06-14-2005 11:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration