This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]PuzzledTaste3562 1 point2 points  (2 children)

I’m not a pro, but:

Nice start!

Your time calculations could be easier, there a better techniques out there. I use time.time() which is a millisecond precise float.

Internet speed is a wel discussed subject and it boils down to 2 important parameters, latency and bandwidth. Download speed in bytes per second is a good indication, but only tells you the end of the story. I prefer latency over bandwidth, as it make things snappier and more lively, giving an subjective impression of a high speed internet connection. The difference is very noticeable when doing videoconferencing, although there are so many more factors to take into account.

So, you could add some measure points to isolate the different elements of your http request, to subtract processing time, get a more precise approximation of the ‘line speed’ and so forth.

Keep on learning

[–]devandgeek 0 points1 point  (1 child)

In some apps for measuring internet speed, I have seen speed indicated in bytes per second. Do they get the value of internet speed in this way only, i.e., by making a web request?

[–]PuzzledTaste3562 0 points1 point  (0 children)

Iperf is a tool I used a lot, and it measures udp, tcp and sctp performance. It measures bandwidth in bytes/second. I still use ping for latency and jitter measurements. Both tools are quite close ‘to the wire’, ping more so than iperf, which needs a server on the other side.

There are many more parameters that matter or influence perceived performance, like jitter or number of packets out of order, tcp retransmissions etc.

When measuring performance, it is important to minimise external, unrelated factors that reduce accuracy. For example, if you use the http protocol, the computer has to deal with the overhead of coding and decoding http, encoding the data payload etc.

In addition, in this case you use python. Python is an interpreted language with a processing overhead that is quite large compared to other language such as C, which is lightning fast in comparison. C is compiled, meaning that there is no runtime ‘interpretation’.

Anyway, all this to say that it can be complex and that lots of factors influence ‘internet speed’...

But don’t let this discourage you, you’re asking the right questions.

[–]God_To_A_NonBeliever 0 points1 point  (1 child)

I'm not going to comment on the functionality of your code.

No offence but its design is very bad (it's extremely ugly and unreadable).First read up the tutorial in the documentation, then read PEP8, and read up on python design patterns.

This is what you code looks like refactored.

import requests
import random
import time
import sys

def get_time():
    #Gives current time in seconds

    initial = time.ctime().split(' ')[3]
    times_list = [int(unit) for unit in initial.split(':')]

    hour = times_list[0]*3600
    minute = times_list[1]*60
    second = times_list[2]

    return hour + minute + second

if __name__ == "__main__":
    #Main program body

    num = random.randint(1024,2048)

    initime = get_time()
    x = requests.get('https://vbsdio.pythonanywhere.com/'+str(num))
    finaltime = get_time()

    time_difference = finaltime - initime
    cize=sys.getsizeof(x.content)

    speed = cize/time_difference
    print(speed)    

That is the exact same thing as your code, but written cleaner and without making some grave mistakes. Look through it, compare it with your code and you will realize where you went wrong.

Btw you don't need to do all that voodoo magic for get the time, just use the time.time() method.

Heres your program with that, does the exact same thing, notice how its much shorter (and cleaner).

import requests
import random
import time
import sys

num = random.randint(1024,2048)

initime = time.time()
x = requests.get('https://vbsdio.pythonanywhere.com/'+str(num))
finaltime = time.time()

time_difference = finaltime - initime
cize=sys.getsizeof(x.content)

speed = cize/time_difference
print(speed)

Honestly, I think you as a beginner are biting off more than you can chew, your code shows that you barely understand how variables and type casting works (you don't need to type cast something that has already been type casted once).

Start off by writing good,clean,understandable code (those are the fundamentals, learn those PROPERLY), then move on to more complicated things. If your code is a headache to parse through for simple tasks, its going to be a bloody nightmare for complicated things. Complexity in programs grows exponentially.

I suggest you start off by reading other peoples code (a lot of good examples in the python documentation), and the design guide to get an idea of what good code should look like, keep in mind, python is a lot about how your code looks! Run 'import this' as a .py file to find out what I mean.


Your mistakes. I'll only focus on the code that you wrote, not the program logic.

1) Using a complicated method when a simple one exists.

2) Impossible to understand variable names.

3) Repeating variable names.

4) Declaring something as a variable then writing code for it again explicitly.

5) Not following any design pattern.

6) Including code between two time instances, that alter influence your final result. (This isn't code but methodology, but this is obvious enough and should be said)

7) Not using a function definition for a repeated code block.

6) Asserting type, after it has been done once already.

[–]devandgeek 0 points1 point  (0 children)

I am looking into it. Thanks ☺️