[think #2] timeit, operated vs operate in function

kamchur·2022년 8월 1일
0

😁START

Always, I wander operate speed more than better,
which implement function after operated value? or operate valye in function?

example.

a = math.pi * 999
count = 0
for i in range(a):
	if i % 3 == 0:
    	count += 1
        
print(count)

I wander, if `math.pi * 9` operate how to do in range function?
0  import timeit
1  import math
2  
3  def test1():
4      a = int(math.pi * 999)
5      count = 0
6      for i in range(a):
7          if i % 3 == 0:
8              count += 1
9      # print(count)
10 
11 def test2():
12     count = 0
13     for i in range(int(math.pi * 999)):
14         if i % 3 == 0:
15             count += 1
16 
17 
18 
19 t1 = timeit.timeit('test1()', setup="from __main__ import test1", number=90000)
20 t2 = timeit.timeit('test2()', setup="from __main__ import test2", number=90000)
21 print(t1)
22 print(t2)

result

# 1. 19 line, timeit set parameter number=10000
1.3534549
1.4153422

# 2. 19 line, timeit set parameter number=10000
1.3587663
1.3959836

# 3. 19 line, timeit set parameter number=90000
12.3827818
13.016218799999999

I know operated value speed more than operate in function !
would using code operated varaiable.


so, I have one more question
if loop affects speed?

test1(), test2() functions add 3 if loop test
add code.

11 def test1_3if():
12     a = int(math.pi * 999)
13     count = 0
14     count1 = 0
15     count2 = 0
16     for i in range(a):
17         if i % 3 == 0:
18             count += 1
19         elif i % 2 == 0:
20             count1 += 1
21         else:
22             count2 += 1

31 def test2_3if():
32     count = 0
33     count1 = 0
34     count2 = 0
35     for i in range(int(math.pi * 999)):
36         if i % 3 == 0:
37             count += 1
38         elif i % 2 == 0:
39             count1 += 1
40         else:
41             count2 += 1
42 
43 
44 t1 = timeit.timeit('test1()', setup="from __main__ import test1", number=90000)
45 t1_if = timeit.timeit('test1_3if()', setup="from __main__ import test1_3if", number=90000)
46 t2 = timeit.timeit('test2()', setup="from __main__ import test2", number=90000)
47 t2_if = timeit.timeit('test2_3if()', setup="from __main__ import test2_3if" ,number=90000)
48 print(t1)
49 print(t1_if)
50 print("-------")
51 print(t2)
52 print(t2_if)

result

# 1.
12.636524
23.949876600000003
-------
14.169879300000005
25.9566776   

# 2.
12.7316927
24.785127900000003
-------
14.076964199999999
25.846579499999997

# 3.
12.7747608
24.0340681
-------
13.675656700000005
24.7609008

wow.. add if loop affects slowing down


I have one more question
before, if loop was if ~ elif ~ else
I wander if, if, if loop

# add 2 code

def test1_3ifif():
    a = int(math.pi * 999)
    count = 0
    count1 = 0
    count2 = 0
    for i in range(a):
        if i % 3 == 0:
            count += 1
        if i % 2 == 0:
            count1 += 1
        if i % 5 == 0:
            count2 += 1
            
def test2_3ifif():
    count = 0
    count1 = 0
    count2 = 0
    for i in range(int(math.pi * 999)):
        if i % 3 == 0:
            count += 1
        if i % 2 == 0:
            count1 += 1
        if i % 5 == 0:
            count2 += 1
            

t1 = timeit.timeit('test1()', setup="from __main__ import test1", number=20000)
t1_if = timeit.timeit('test1_3if()', setup="from __main__ import test1_3if", number=20000)
t1_ifif = timeit.timeit('test1_3ifif()', setup="from __main__ import test1_3ifif", number=20000)

t2 = timeit.timeit('test2()', setup="from __main__ import test2", number=20000)
t2_if = timeit.timeit('test2_3if()', setup="from __main__ import test2_3if" ,number=20000)
t2_ifif = timeit.timeit('test2_3ifif()', setup="from __main__ import test2_3ifif" ,number=20000)
print(t1)
print(t1_if)
print(t1_ifif)
print("-------")
print(t2)
print(t2_if)
print(t2_ifif)

result.

# change value number=20000
# 1.
2.8760593
4.9170562        
7.5904838        
-------
2.772185900000002
5.317933100000001
8.210059100000002

# 2.
2.8960647
5.5680559
8.2011221
-------  
2.9109318
5.439665399999999
8.5901228

# 3.
2.8950784
5.3960216999999995
8.691413499999998
-------
3.0743781000000006
5.7700678
9.032289500000001

wow ... add if loop is similar in speed to each other.
if ~ elif ~ else is faster than if ~ if ~ if

so funny... !!!

I will use to code operated varaiable values !


😂END

2022.08.01. first commit
profile
chase free

0개의 댓글