Reputation: 11
I have the following program which generates 100 million mac's and append it into the list.
But the 100 million mac's generation and insertion itself takes ~7 Minutes in python???
import datetime
def mac_gen():
hex_byte1=0
hex_byte2=0
hex_byte3=0
hex_byte4=0
hex_byte5=0
hex_byte6=0
hex_byte7=0
hex_byte8=0
hex_byte9=0
hex_byte10=0
hex_byte11=0
hex_byte12=0
total_mac = 0
for hex_byte_12 in range(0,16):
for hex_byte11 in range(0,16):
for hex_byte10 in range(0,16):
for hex_byte9 in range(0,16):
for hex_byte8 in range(0,16):
for hex_byte7 in range(0,16):
for hex_byte6 in range(0,16):
for hex_byte5 in range(0,16):
for hex_byte4 in range(0,16):
for hex_byte3 in range(0,16):
for hex_byte2 in range(0,16):
for hex_byte1 in range(0,16):
total_mac +=1;
if total_mac > number_of_mac_to_print:
return
mac_list.append("%X%X:%X%X:%X%X:%X%X:%X%X:%X%X" %(hex_byte_12,hex_byte11,hex_byte10,hex_byte9,hex_byte8,hex_byte7,hex_byte6,hex_byte5,hex_byte4,hex_byte3,hex_byte2,hex_byte1))
mac_list=list()
number_of_mac_to_print = 100000000
print(datetime.datetime.utcnow())
mac_gen()
print(datetime.datetime.utcnow())
print(len(mac_list))
2018-03-09 07:15:41.650460 <--- time stamp before calling mac_gen method
2018-03-09 07:22:33.902744 <--- time stamp after calling mac_gen method
100000000 . <---- total entries. 100 million
so question is:
how to reduce the running time from 7 minutes to lowest as possible in python?
if the same problem i am solving in C or C plus plus give please give me generrfic solution for that as well?
when running the same code for 1 Billion mac it is terminating automatically i belive. and taking much more time. how to achive for 1 Billion mac generation?
Thanks in advance
Upvotes: 1
Views: 637
Reputation:
Are you sure you want to do that ? All 12
digit hex numbers would fill up the largest supercomputer (281474976710656
values). Actually, all first 100000000
addresses have a zero for the first five digits (as 16^7 = 268435456
).
Do you really need to store all these addresses ? If yes, I would recommend to use a single range(100000000)
and leave the values as integers, only convert to hex when needed. This will spare a lot of space.
If not possible, it is probably more efficient to work with a string of 12
hex characters that you increment yourself rather than using a costly formatting operation.
And please, don't store those 500000000
':'
useless separators.
Upvotes: 1
Reputation: 24547
The long running times you're experiencing are quite likely to result from memory being paged out to your hard drive in order to make room for the addition of new items to mac_list
.
I can't think of any good reason why you would need to store all these MACs in memory. They're numbered consecutively, so you can easily generate them on the fly:
gen_mac = lambda n: ":".join([("%012X" % n)[i:i+2] for i in range(0,12,2)])
Then instead of fetching items from mac_list
, just use this function instead. In other words, replace
m = mac_list[12345678]
with
m = gen_mac(12345678)
Upvotes: 5
Reputation: 3745
The following code takes 3:20 on my laptop
mac_list=list()
for i in range(0, 100000000):
strHex = hex(i)[2:].rjust(12, '0')
mac_list.append(strHex[0:2] + ":"+strHex[2:4] + ":"+strHex[4:6] + ":"+strHex[6:8] + ":"+strHex[8:10] + ":"+strHex[10:12])
Upvotes: 1