PRVS
PRVS

Reputation: 1690

Measuring latency over network

I have a program that will run in two different computers like: Server and Client. I want to measure the latency over the network in my program in C++ and Java. The normal commands to measure the runtime (such as gettimeofday in C) I think are not very accurate for run latency over the network. Some advice?

Upvotes: 2

Views: 3534

Answers (1)

Pavel P
Pavel P

Reputation: 16996

You can measure accurate time with std::chrono

#include <chrono>

auto timeStart = std::chrono::high_resolution_clock::now();
// do something... wait for result...
auto timeEnd = std::chrono::high_resolution_clock::now();

long long duration = std::chrono::duration_cast<std::chrono::microseconds>(timeEnd - timeStart).count();

Replace microseconds with nanoseconds, milliseconds, seconds to get duration in different units.

You may as well use std::clock though it might provide less accurate measurements:

#include <ctime>

std::clock_t timeStart = std::clock();
// do something... wait for result...
std::clock_t timeEnd = std::clock();

double durationMs = 1000.0 * (timeEnd-timeStart) / CLOCKS_PER_SEC;

Upvotes: 5

Related Questions