LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 04-10-2011, 09:51 PM   #1
endof
LQ Newbie
 
Registered: Nov 2009
Location: Turkey
Distribution: Ubuntu
Posts: 4

Rep: Reputation: 0
nanosecond calculate difference


ı need to calculate how many nanosecond the process takes . must give the same value every time (c++)
ubuntu 10.10
my system information------------------------------------------------------
*-cpu
description: CPU
product: Intel(R) Core(TM)2 Duo CPU T7300 @ 2.00GHz
vendor: Intel Corp.
physical id: 400
bus info: cpu@0
slot: Microprocessor
size: 2001MHz
capacity: 2001MHz
width: 64 bits
clock: 200MHz
*-cache:0
description: L1 cache
physical id: 700
size: 32KiB
capacity: 32KiB
capabilities: internal write-back data
*-cache:1
description: L2 cache
physical id: 701
size: 4MiB
capacity: 4MiB
clock: 66MHz (15.0ns)
capabilities: pipeline-burst internal varies unified
*-memory
description: System Memory
physical id: 1000
slot: System board or motherboard
size: 4GiB
*-bank:0
description: DIMM DDR Synchronous 667 MHz (1.5 ns)
product: 9905295-051.A00LF
vendor: Kingston
physical id: 0
serial: 53CC4D13
slot: DIMM_A
size: 2GiB
width: 64 bits
clock: 667MHz (1.5ns)
*-bank:1
description: DIMM DDR Synchronous 667 MHz (1.5 ns)
product: 9905295-051.A00LF
vendor: Kingston
physical id: 1
serial: 51CC4E13
slot: DIMM_B
size: 2GiB
width: 64 bits
clock: 667MHz (1.5ns)

Code-----------------------------------------------------------------------
#include <stdio.h>
#include <time.h>

int main ()
{
struct timespec t1;
struct timespec t2;

clock_gettime(CLOCK_REALTIME, &t1);
clock_gettime(CLOCK_REALTIME, &t2);
printf ("CLOCK_REALTIME : %ld\n", t2.tv_nsec-t1.tv_nsec );

clock_gettime(CLOCK_MONOTONIC, &t1);
clock_gettime(CLOCK_MONOTONIC, &t2);
printf ("CLOCK_MONOTONIC : %ld\n", t2.tv_nsec-t1.tv_nsec );

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
printf ("CLOCK_PROCESS_CPUTIME_ID : %ld\n", t2.tv_nsec-t1.tv_nsec );

clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t1);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t2);
printf ("CLOCK_THREAD_CPUTIME_ID : %ld\n", t2.tv_nsec-t1.tv_nsec );

}
-----------------------------------------------------------------
compile use -lrt
"gcc -lrt example.c -o example"

first result ----------------------------------------------------
CLOCK_REALTIME : 1118
CLOCK_MONOTONIC : 1187
CLOCK_PROCESS_CPUTIME_ID : 736
CLOCK_THREAD_CPUTIME_ID : 396

second result
CLOCK_REALTIME : 1187
CLOCK_MONOTONIC : 1117
CLOCK_PROCESS_CPUTIME_ID : 647
CLOCK_THREAD_CPUTIME_ID : 411

Last edited by endof; 04-11-2011 at 08:25 AM.
 
Old 04-10-2011, 10:22 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

Code:
must give the same value every time
Find an OS that runs your program as its only task.




Cheers,
Tink
 
Old 04-10-2011, 10:32 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
I am with Tink on this one. If you can guarantee no other tasks will take any of the cpu, ram or disk time away from your task then "maybe"
you will get the same times or perhaps close. Especially when measuring at such a low level.
 
Old 04-10-2011, 10:58 PM   #4
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 430
Blog Entries: 28

Rep: Reputation: 2
you could record the times and MAYBE do a normal distribution, so you can draw a conclusion like with a mean 530 sd 50, 600 lies above 80% or something. IF you really want a statistical analysis. But its impossible to give a exact consistency even to seconds.
Ted

Last edited by ted_chou12; 04-10-2011 at 11:00 PM.
 
Old 04-11-2011, 08:10 AM   #5
endof
LQ Newbie
 
Registered: Nov 2009
Location: Turkey
Distribution: Ubuntu
Posts: 4

Original Poster
Rep: Reputation: 0
nanosecond calculate

Quote:
Originally Posted by endof View Post
ı need to calculate how many nanosecond the process takes . must give the same value every time (c++)
ubuntu 10.10
my system information------------------------------------------------------
*-cpu
description: CPU
product: Intel(R) Core(TM)2 Duo CPU T7300 @ 2.00GHz
vendor: Intel Corp.
physical id: 400
bus info: cpu@0
slot: Microprocessor
size: 2001MHz
capacity: 2001MHz
width: 64 bits
clock: 200MHz
*-cache:0
description: L1 cache
physical id: 700
size: 32KiB
capacity: 32KiB
capabilities: internal write-back data
*-cache:1
description: L2 cache
physical id: 701
size: 4MiB
capacity: 4MiB
clock: 66MHz (15.0ns)
capabilities: pipeline-burst internal varies unified
*-memory
description: System Memory
physical id: 1000
slot: System board or motherboard
size: 4GiB
*-bank:0
description: DIMM DDR Synchronous 667 MHz (1.5 ns)
product: 9905295-051.A00LF
vendor: Kingston
physical id: 0
serial: 53CC4D13
slot: DIMM_A
size: 2GiB
width: 64 bits
clock: 667MHz (1.5ns)
*-bank:1
description: DIMM DDR Synchronous 667 MHz (1.5 ns)
product: 9905295-051.A00LF
vendor: Kingston
physical id: 1
serial: 51CC4E13
slot: DIMM_B
size: 2GiB
width: 64 bits
clock: 667MHz (1.5ns)

Code-----------------------------------------------------------------------
#include <stdio.h>
#include <time.h>

int main ()
{
struct timespec t1;
struct timespec t2;

clock_gettime(CLOCK_REALTIME, &t1);
clock_gettime(CLOCK_REALTIME, &t2);
printf ("CLOCK_REALTIME : %ld\n", t2.tv_nsec-t1.tv_nsec );

clock_gettime(CLOCK_MONOTONIC, &t1);
clock_gettime(CLOCK_MONOTONIC, &t2);
printf ("CLOCK_MONOTONIC : %ld\n", t2.tv_nsec-t1.tv_nsec );

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t2);
printf ("CLOCK_PROCESS_CPUTIME_ID : %ld\n", t2.tv_nsec-t1.tv_nsec );

clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t1);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t2);
printf ("CLOCK_THREAD_CPUTIME_ID : %ld\n", t2.tv_nsec-t1.tv_nsec );

}
compile use -lrt
"gcc -lrt example.c -o example"

first result
CLOCK_REALTIME : 1118
CLOCK_MONOTONIC : 1187
CLOCK_PROCESS_CPUTIME_ID : 736
CLOCK_THREAD_CPUTIME_ID : 396

second result
CLOCK_REALTIME : 1187
CLOCK_MONOTONIC : 1117
CLOCK_PROCESS_CPUTIME_ID : 647
CLOCK_THREAD_CPUTIME_ID : 411
end
 
0 members found this post helpful.
Old 04-11-2011, 09:02 AM   #6
endof
LQ Newbie
 
Registered: Nov 2009
Location: Turkey
Distribution: Ubuntu
Posts: 4

Original Poster
Rep: Reputation: 0
who know not multi-thread linux version
 
Old 04-11-2011, 09:05 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
Was there something you are trying to say? No amount of data based on your machine features will help this scenario.
You can try to include the timings of all things running on your machine, but of course then the issue is what does the scheduler in
the cpu choose to give priority to?? As has been mentioned by a few people now, depending on what is running on your system whilst executing
your code will affect the timings, even if only slightly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Calculate the hours difference b/n timezones in C/C++ sreeni_dd Programming 2 04-12-2012 03:14 AM
bash: time difference and calculate the mid day of the day? frenchn00b Programming 3 05-03-2009 03:03 PM
how to calculate time difference in milliseconds in C/C++ waqasdaar Programming 19 03-17-2009 05:02 PM
calculate time difference between 2 formatted timestamps nickleus Linux - General 3 05-12-2006 11:08 PM
nanosecond timing cyb0rg777 Programming 8 02-16-2006 07:43 PM

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

All times are GMT -5. The time now is 08:20 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration