The other day I haloed to python
, so I went back to the beginning and did something like a basic exercise
I tried to do something like "the average of the sums from 1 to 10"
Introduction of the solution at that time
# -*- coding: utf-8 -*-
def normal_average(start, end):
#0 to be float when calculating average.Initializing to 0
sum = 0.0
size = end - start + 1
for i in range(start, end + 1):
sum += i
else:
print "normal_average = %f"%(sum/size)
start = 1
end = 10
normal_average(start, end)
result normal_average = 5.500000
There is a feeling that the + 1
part is not good, but it is okay
python
I'm a beginner, so the bad things about it are cute
def another_average(start, end):
print "another_average = %f"%((start + end)/2.0)
start = 1
end = 10
another_average(start, end)
result another_average = 5.500000
I thought that the average of the sum of consecutive integers shouldn't be a loop without turning it, so I wrote it. I wonder if this way of solving is also an ant This should make a difference in processing speed as the number increases.
The larger the number, the greater the difference, so this time I will try to find the average of the sums from 1 to 100 million
.
# -*- coding: utf-8 -*-
import time
def normal_average(start, end):
sum = 0.0
size = end - start + 1
for i in range(start, end + 1):
sum += i
else:
print "normal_average = %f"%(sum/size)
def another_average(start, end):
print "another_average = %f"%((start + end)/2.0)
#Main executive
start = 1
end = 100000000
#Measurement of execution time
normal_start_time = time.time()
normal_average(start, end)
normal_end_time = time.time()
print "execute time is %f"%(normal_end_time - normal_start_time)
print
another_start_time = time.time()
another_average(start, end)
another_end_time = time.time()
print "execute time is %f"%(another_end_time - another_start_time)
result normal_average = 50000000.500000 execute time is 12.961456 another_average = 50000000.500000 execute time is 0.000006
(Isn't it overwhelming? Our army ...!) Obviously, the processing speed is different.
It is said that if you change the logic in the first place, you may get overwhelmingly fast results. Of course, this solution is not without problems, but at least it is required faster than looping to get the "average of the sum of consecutive integers". It was a lesson that it was important to change the viewpoint and solve the problem.