Statistics
| Branch: | Revision:

root / src / sim / cstopwatch.cc @ e26d3d25

History | View | Annotate | Download (1.78 KB)

1
//==========================================================================
2
//  CSTOPPWATCH - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  Declaration of the following classes:
8
//    cStoppwatch: measures the time interval between invocations
9
//
10
//==========================================================================
11

    
12
/*--------------------------------------------------------------*
13
 Copyright (C) 2010 Georg Kunz
14

15
 This file is distributed WITHOUT ANY WARRANTY. See the file
16
 `license' for details on this and other legal matters.
17
 *--------------------------------------------------------------*/
18

    
19
#include "cstopwatch.h"
20
#include <math.h>
21
#include <iostream>
22

    
23
cStopWatch::cStopWatch()
24
    : running(false)
25
    , calibrated(false)
26
{
27
    reset();
28
}
29

    
30
bool cStopWatch::calibrate()
31
{
32
    double elapsedTime = 0;
33
    unsigned long long timeSpecSum;
34

    
35
    unsigned long long startTicks = 0;
36
    unsigned long long stopTicks = 0;
37
    unsigned long long elapsedTicks = 0;
38

    
39
    double dummyResult;
40

    
41
    for (int i = 0; i < 10; i++)
42
    {
43
        start();
44
        startTicks = rdtsc();
45

    
46
        for (int j = 0; j < 100; j++)
47
        {
48
            dummyResult = sin(i) * cos(j) * tan(i * j);
49
        }
50

    
51
        stop();
52
        stopTicks = rdtsc();
53

    
54
        timespec elapseTimeSpec = getTime();
55
        timeSpecSum = elapseTimeSpec.tv_sec + elapseTimeSpec.tv_nsec;
56
        elapsedTime += timeSpecSum / 1000000000.0;
57

    
58
        elapsedTicks += stopTicks - startTicks;
59

    
60
        reset();
61
    }
62

    
63
    // average
64
    elapsedTime /= 10;
65
    elapsedTicks /= 10;
66
    ticksPerSecond = (unsigned long long) (1.0 / elapsedTime * elapsedTicks);
67

    
68
    std::cout << "TSC calibration done: 1 seconds == " << ticksPerSecond
69
            << " ticks" << std::endl;
70

    
71
    return true;
72
}