Elle Kelsey, September 5th, 2023

Introduction

Choosing the right clock or timer depends entirely on its purpose and the required results. It's important to consider the desired resolution. In a project involving sending UDP packets through a socket protocol in C, we had to identify a precise and accurate clock to use. This prompted us to explore the various available options.

Procedure

We wrote a simple program to send an equal number of packets at two different time shifts. This results in a graph with two distinct peaks which gives some intuition through visuals about how the chosen clock is preforming. The program also produces some statistics including standard deviation, mean, and variance. For our purposes, we were comparing python to C.

Scouring the internet for nanosecond resolution clocks resulted in the conclusion to try calculating time based on clocks per second using the following code.

double getTime(void)
{
    double time;
    time = (double) clock();  // get initial time
    time = time/CLOCKS_PER_SEC;  // in seconds
    return time;
}

Results of C code

The graphs we made from running the C code, were surprising. They were never as tight as the python graphs and varied from each other quite a bit.

https://lh3.googleusercontent.com/t2VtcujCc5S7RJ8KqT9-jdQvs6Jyb8P4znd2GBirXoc09LwlG2EOhAgmOoQqwCrqvAcRi2ANrExjBvnlI0iafsDLFFB_pX_gpIf0ChQ0j8JIPWrX2rV2k0FMU-cgg0QSPGTxevHHqtabkmTLIlZOOnH7CQ=s2048

https://lh4.googleusercontent.com/NTgGzb7ir_-_AQ1eu1zSmdSIDTIPAexqhHkyyNH-AKDno4UI7YygLvlI6ZmdRjdaQ3npH8HNutR-tiAfS1Mu8VAXO5NwpZvWgVUiTbl1bTPzlqXEVr_1CJUL64dNPSWPn9dCrXB704dLCPz1gKwxcvfE0g=s2048

Results of Python Code:

The graphs resulting from the python code looked much tighter and were always consistent between each other.

https://lh6.googleusercontent.com/IP1xZSFGHrQd_5B-4K6frSDORvH2g7xA3uPUIreLKhQROLkCpxMWxOKbn8AzvmAs9U77azPxb0xAcEZiC_0MdK236IL6eXKNIKdqZRWM8Xw1uK86QKDn2lLvFvB0vHVBtAWWpVOHYHl3rdDgVoD2TxKraA=s2048

Averages for Python and C:

Categories Avg Mean Avg StDev Avg Variance
Left bin Py 0.02463155678 3.42664883e-04 1.17704879e-07
Left bin C 0.02628034544 9.7282697e-04 .00693291e-06
Right bin Py 0.02335810312 3.33108101e-04 1.07781039e-07
Right bin C 0.0217278651 9.76876519e-04 1.01743708e-06

These results led us to conclude that python’s clock modules and are more precise/accurate than C. These results contradict our predictions so we investigate further.

Changing clock approaches

The clock used for the previous experiments in C reports CPU time which introduces inconsistencies and is not nearly as precise or accurate as it claims.