Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Tuesday, August 20, 2013

time command

root@server102:/etc/init.d# time ./apache2 start
 * Starting web server apache2                                                              [ OK ]

real 0m0.034s
user 0m0.006s
sys 0m0.016s

http://zch051383471952.blogspot.com/2010/01/different-of-real-user-sys-time.html


Real refers to actual elapsed time; User and Sys refer to CPU time used only by the process.
  • Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete).
  • User is the amount of CPU time spent in user-mode code (outside the kernel) within the process. This is only actual CPU time used in executing the process. Other processes and time the process spends blocked do not count towards this figure.
  • Sys is the amount of CPU time spent in the kernel within the process. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space. Like 'user', this is only CPU time used by the process.



Friday, March 25, 2011

rdtsc in Windows and Linux

Code in Windows, you have to compile it by VC++

#include <intrin.h>
unsigned __int64 rdtsc()
{
  return __rdtsc();
}


Code in Linux 

#include <stdint.h>
#include <stdio.h>

  __inline__ uint64_t rdtsc() {
    uint32_t lo, hi;
    __asm__ __volatile__ (      // serialize
    "xorl %%eax,%%eax \n        cpuid"
    ::: "%rax", "%rbx", "%rcx", "%rdx");
    /* We cannot use "=A", since this would use %rax on x86_64 */
    __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
    return (uint64_t)hi << 32 | lo;
  }
int main()
{
    printf("rdtsc is: %llu\n",rdtsc());
    return 1;
}

more detailed information: http://en.wikipedia.org/wiki/Time_Stamp_Counter

Another version in Linux:
/************ Record TSC to SMRAM offset 0xA9000 ****************/
movl $0xA9000, %ebx
rdtsc
movl %eax, (%ebx)
movl %edx, 4(%ebx)

unsigned int lo = *(unsigned int*)(0xA9000);
unsigned int hi = *(unsigned int*)(0xA9004);
unsigned long long beforeConfig = (unsigned long long)hi << 32 | lo;

Thursday, March 24, 2011

Print out current time in C on Windows

  typedef struct _SYSTEMTIME {
       WORD wYear;
       WORD wMonth;
       WORD wDayOfWeek;
       WORD wDay;
       WORD wHour;
       WORD wMinute;
       WORD wSecond;
       WORD wMilliseconds;
    } SYSTEMTIME;

     #include <Windows.h>
     #include <stdio.h>

     void main()
     {
         SYSTEMTIME st;
         GetSystemTime(&st);
         printf("Year:%d\nMonth:%d\nDate:%d\nHour:%d\nMin:%d\nSecond:% d\n" ,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
     }


more info:
http://www.codersource.net/c/c-tutorials/c-date-and-time.aspx
 

Tuesday, September 21, 2010

Print current Time

How to print current time in shell script:
$date

For example, if you want to print current seconds since Epoch, and current nanoseconds. %s means print seconds, and %N means print nanoseconds

$date +%s%N

If you want to see the details, please see man page of date


How to print current time in C:
You could use time() function, please see man(3) page
 time_t could print out by using %ld

 e.g.
time_t result;
result = time(NULL); // or time(&result);
printf("%ld", result); // or printf("%s\n", ctime(&result))

If you want to print out the sec and nsec of current time.

e.g.
#include <time.h>
void printCurrentTimeNsec()
{
  struct timespec tp;
  clock_gettime(CLOCK_REALTIME, &tp);
  printf("%ld  %ld\n", tp.tv_sec, tp.tv_nsec);
}

When you compile this program, you need to add -lrt because of clock_gettime() function.