What is the best way to calculate the percentage of a repeat operation?

I wrote a function that saves all the numbers between two digital groups into a text file, with a step to save some space and time, and I could not figure out how to show the percentage value, so I tried this.

for length in range(int(limit_min), int(limit_max) + 1): percent_quotient = 0 j=0 while j <= (int(length * "9")): while len(str(j)) < length: j = "0" + str(j) percent_quotient+=1 j = int(j) + int(step) # increasing dummy variable for length in range(int(limit_min), int(limit_max) + 1): counter=1 i = 0 while i <= (int(length * "9")): while len(str(i)) < length: i = "0" + str(i) # print "Writing %s to file. Progress: %.2f percent." % (str(i),(float(counter)/percent_quotient)*100) a.write(str(i) + "\n") # this is where everything actually gets written i = int(i) + int(step) # increasing i counter+=1 if length != int(limit_max): print "Length %i done. Moving on to length of %i." % (length, length + 1) else: print "Length %i done." % (length) a.close() # closing file stream print "All done. Closed file stream. New file size: %.2f megabytes." % (os.path.getsize(path) / float((1024 ** 2))) print "Returning to main..." 

What I tried to do here was to have the program iterate as many times as it usually did, but instead of writing to a file, I just made the percent_quotient variable how many times the iteration repeats, (I called j dummy variable, since it is there only to break the loop, sorry if there is another expression for this.) The second part is the actual work, and I put the counter variable and I divide it by percent_quotient and multiply by 100 to get the percentage.

The problem is that when I tried to make a dictionary from 1 to 8 in length, it actually took a minute to count everything. I guess it will take much longer if I want to make an even larger dictionary.

My question is, is there a better way to do this faster?

+5
source share
2 answers

Well, the step variable gives me a big headache, but without it it would be the right way to calculate how many numbers will be written.

 percent_quota=0 #starting value for i in range(limit_min,limit_max+1): #we make sure all lengths are covered percent_quota+=(10**i)-1 #we subtract 1 because for length of 2, max is 99 

TessellatingHeckler, thanks, your answer helped me figure this out!

0
source

I can’t understand what this does. But it looks like something like this:

 a = file('d:/whatever.txt', 'wb') limit_min = 1 limit_max = 5 step = 2 percent_quotient = (10 ** (limit_max - limit_min)) / step for i in range(limit_min, 10**limit_max, step): output = str(i).zfill(limit_max) + '\r\n' a.write(output) if i % 100 < 2: print "Writing %s to file. Progress: %.2f percent." % (str(i),(float(i)/percent_quotient)*100) a.close() 

If this is correct, I suggest:

  • Do less code cycles and more math
  • Use string.zfill() instead of while len(str(num)) < length: "0" + str(num)
  • Do not overload the console with the outputs of each individual number, just print a status update every 100 numbers or every thousand numbers or so.
  • Make less str(int(str(int(str(int(str(int(...
  • Avoid "" + blah inside hard loops if possible, this causes the lines to be rebuilt every time, and this is especially slow.
+3
source

All Articles