0

how to reduce the time for code execution?

import ipaddress import os ipnet=ipaddress.ip_network('192.12.0.0/24') for i in ipnet.hosts(): response=os.system('ping -c 1 ' + str(i)) if response == 0: print(i, " alive") else: print(i, " down") This is a script for ping whether the host is up or not. But the ping executes very slowly one by one per host or IP. Should I use multi threading or anything else? Isn't it possible to get output quickly?

21st May 2017, 1:40 PM
chiju
chiju - avatar
5 ответов
+ 1
provided that your code is ok first I'd check internet speed, then the machine for overloading. processors are light years faster then network infrastructure, especially in home use
25th May 2017, 6:12 PM
Skipper
Skipper - avatar
+ 1
never heard about parallel internet protocol in common use. I still believe u have a mess in machine and os/card configuration and a slow internet connection. true parallel pinging requires several machines and subsets of pinged address. in general speeds relate to computer speeds and network speeds. what's the point in having a strong machine and a dial-in access + pinged servers in the other hemisphere. true parallel processing consists of chunks of algorithm processed by different cores inside a single infrastructure
28th May 2017, 9:53 PM
Skipper
Skipper - avatar
+ 1
I am not saying your code is ok. It's not. For example -c should be replaced by -n. I am just saying if you want true parallel execution you need at least two machines and two internet connections. I did this, in windows, Python 3.xx import os flag = 0 flag = os.system( "ping -n 1 8.8.8.8 > null") if flag == 0: print ("up") else: print ("down") As I expected in my previous posts there is a significant difference in speed of execution. In console it took ca. 22ms. but in Python nearly 4s. If you need speed write it in ANSI C.
6th Jun 2017, 5:25 AM
Skipper
Skipper - avatar
0
In my script, the aim is to check which of the hosts(IP) are up and which of them are down. How can I check this status in parallel? Currently, the script checks one by one and it takes lots of time to check all the hosts under the given network (192.12.0.0/24)
28th May 2017, 8:42 AM
chiju
chiju - avatar
0
thanks skipper for your continuous responses. So you are saying that I written my script correctly for my requirement. If you could execute my code from your machine to ping a network, that will be great.
5th Jun 2017, 6:24 PM
chiju
chiju - avatar