-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswapTwoNos.py
More file actions
49 lines (35 loc) · 764 Bytes
/
swapTwoNos.py
File metadata and controls
49 lines (35 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import timeit
def swap1(a,b):
temp = a
a= b
b = temp
def swap2(a, b):
a = a * b
b = a / b
a = a / b
def swap3(a, b):
a = a + b
b = a - b
a = a - b
def swap4(a, b):
a = a ^ b
b = a ^ b
a = a ^ b
def swap5(a,b):
return b,a
t1 = timeit.Timer(lambda: swap1(1,2))
print('Method 1 (Using Temp):',t1.timeit())
t2 = timeit.Timer(lambda: swap2(1,2))
print('Method 2 (Using * and /):',t2.timeit())
t3 = timeit.Timer(lambda: swap3(1,2))
print('Method 3 (Using + and -):',t3.timeit())
t4 = timeit.Timer(lambda: swap4(1,2))
print('Method 4 (Using XOR):',t4.timeit())
t5 = timeit.Timer(lambda: swap5(1,2))
print('Method 5 (Using Python Methods):',t5.timeit())
'''
swap1(1,2)
swap2(1,2)
swap3(1,2)
swap4(1,2)
'''