Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Thursday, January 22, 2015

Profiling Code Using clock_gettime

#include <cstdlib>

#include <iostream>
#include <time.h>

using namespace std;

timespec diff(timespec start, timespec end);

/* Profiling code using clock_gettime
 * Link with the librt library (-lrt)
 */
int main(int argc, char** argv) {

    timespec time1, time2;
    int temp;

    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);

    for (int i = 0; i < 242000000; i++)
        temp += temp;

    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);

    cout << diff(time1, time2).tv_sec << ":" << diff(time1, time2).tv_nsec << endl;

    return 0;
}

/**
 * Returns a timespec of the difference between 2 timespec
 * @param start timespec structure
 * @param end timespec structure
 * @return timespec structure
 */
timespec diff(const timespec start, const timespec end) {
    timespec temp;

    if ((end.tv_nsec - start.tv_nsec) < 0) {
        temp.tv_sec = end.tv_sec - start.tv_sec - 1;
        temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
    } else {
        temp.tv_sec = end.tv_sec - start.tv_sec;
        temp.tv_nsec = end.tv_nsec - start.tv_nsec;
    }

    return temp;
}


Taken from: http://www.guyrutenberg.com/2007/09/22/profiling-code-using-clock_gettime/

Thursday, June 10, 2010

POSIX(R) Threads

Reading "Programming with POSIX(R) Threads".

Asynchronous:
Any two operations are “asynchronous” when they can proceed independently of each other.

Amazon wish list

“If seven maids with seven mops
Swept it for half a year,
Do you suppose,” the Walrus said,
“That they could get it clear?”
“I doubt it,” said the Carpenter,
And shed a bitter tear.

—Lewis Carroll, Through the Looking-Glass


  • pthread_t thread;
  • int pthread_equal (pthread_t t1, pthread_t t2);
  • int pthread_create ( pthread_t *thread, const pthread_attr_t *attr, void *( *start) (void *), void *arg);
  • pthread_t pthread_self (void);
  • int sched_yield ( void);
  • int pthread_exit ( void *value_ptr);
  • int pthread_detach ( pthread_t thread);
  • int pthread_join ( pthread_t thread, void **value_ptr);