Friday 16 November 2018

IP Address Generator Code

It was during one of my own personal Sherlock Holmes like cocaine frenzy moments where i crave for work, work and work (oh!, and i think i forget to mention that i use Kali Linux in practically all my work, unless it's gaming) and where the lines between reality and imagination blurs and fades into oblivion. I think i was fatigued from the many days i sometimes go with without ever sleeping a wink for i don't consider the following work exactly imaginatively impressive.
But i did it anyway.
Initially the problem was making a c++ program that could generate and display all the possible IP addresses available starting from 0.0.0.0 to 255.255.255.255. The task later evolved into comparing how that could be achieved in c++ code and in python code. Then there's comparing which one is faster at executing the task.


The Code

Well, below is  the c++ code;

#include <iostream>
#include <ctime>

using namespace std;

const int MIN = 255
const int MAX = MIN + 1;
int a,b,c,d;


int main(){
    a,b,c,d = 0;
    clock_t start = clock();
    for (int a = 0; a < MAX; a++){
        for (int b = 0; b < MAX; b++){
            for (int c = 0; c < MAX; c++){
                for (int d = 0; d < MAX; d++){
                    cout <<a<<"."<<b<<"."<<c<<"."<<d<<endl;
                }
            }
        }
    }
    clock_t stop = clock();
    double time = static_cast<double>(stop - start)/(CLOCKS_PER_SEC);
    cout <<"Time Taken: "<<time<<" seconds"<<endl;
}


You can copy and paste this in a file and maybe name it ip_gen.cpp, then compile it on the terminal by typing;


g++ ip_gen.cpp -o binary_ip_gen_run

 
That compiles the ip_gen.cpp program into a binary file binary_ip_gen_run which you just run as an executable.

Run execute the binary file by typing on the terminal;

./binary_ip_gen_run


And below is the python code;

from time import time

MIN =255
MAX = MIN + 1

def run():
    a,b,c,d = 0,0,0,0
    start = time()

    for a in range(MAX):
        for b in range(MAX):
            for c in range(MAX):
                for d in range(MAX):
                    print("%d.%d.%d.%d"%(a,b,c,d))
                    ++d
                ++d
            ++d
        ++d

    stop = time()
    tt = stop - start

    print("Time Taken: %s seconds"%(tt))

run()


The code will work whether in python 2 or python 3.
You can save copy and paste this in a file, save it as maybe ip_gen.py and run it from the terminal by typing;


python ip_gen.py


Analysis

Honestly, for the programs to finish generating all IP addresses usually takes so long, like too long for me to simply stay back as i watch their execution(am constantly using my laptop for many other heavy uses which would affect the speed at which the programs get executed at).
So i usually just assign the MIN variable a value of  31 and not 255.


The C++ program took an average of 3.61603 seconds to complete.
The python program on the other hand was a bit interesting. Python 2 took an average of 9.34731936455 seconds while python 3 took an average of about 10.696529150009155 seconds.
This means that c++ is overly faster than python in executing code, which is not a surprise by the way! Assembly would be even faster.


More analysis: c++ is faster than python 2 by about 2.58 times, and faster than python 3 by 2.958 times, nearly 3 times faster. Curiously, python 2 is faster than python 3 by about 1.144 times.
The reason obviously of the speed difference is innate in the languages, c++ is a compiled language, python an interpreted language. Then there's the frustrating python GIL which makes multi-threading for speed quite useless in python. I only recommend multi-threading in python to achieve robustness or multitasking capabilities in a program but never to achieve speed, it simply won't work with speed. The GIL only allows the execution of tasks one at a time, never simultaneously. But that's a discussion for another time.


A major concern however was the execution time difference between python 2 and python 3. Well, why that occurs is still in my Sherlock Holmes to-investigate list. If you happen to know why please do tell.


By the way, you could be interested in running the programs with MIN variable assigned 255 to generate all IP addresses and see how long it takes. If you do please tell me about it.

No comments:

Post a Comment

Let's Talk Anonymity: A Short Treatise On Anonymity

The internet takes a very serious position in our everyday lives. We do a lot of activities over the web, some of which we would like our ...