Jump to content
  • Advertisement
Sign in to follow this  

Calculate download speed with Recv?

This topic is 2270 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


How do you calculate the download speed when recieving data with recv?

recv returns the amount of data read in bytes, add up the total amount of data recieved across multiple calls and divide by the time it took to recieve that data.

Share this post

Link to post
Share on other sites

how do you get the time it took?

it depends on your OS, recv itself doesn't take any time at all.

Basically you do something like this:
startime = getCurrentTimeFromOS();
currenttime = starttime;
lastintervalTime = starttime;
totalBytesdownloaded = 0;
byteslastSecond =0;
while we're not done with the download {
bytesThisRecv = recv(....);
currentTime = getCurrentTimeFromOS();
if (currentTime > lastIntervalTime + 1 second) {
downloadSpeedLastSecond = bytesLastSecond; //this is the average speed across the last second
bytesLastSecond = 0;
lastIntervalTime+= 1 second;
averageSpeed = totalBytes / (currenttime - startime); //this is the total average speed for the download

on Windows you can use timeGetTime to get the current time in milliseconds (~10 ms accuracy normally which should be good enough for measuring download speed) or queryPerformanceCounter for a very high accuracy timer, on Linux you use clock_gettime Edited by SimonForsman

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!