-
Notifications
You must be signed in to change notification settings - Fork 3
/
timing_test.py
74 lines (62 loc) · 2.15 KB
/
timing_test.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Test timing execution with the timeit module
"""
import timeit
from random import randint
# Code to be tested
def generate_random_dna(n):
""" Generate a random DNA sequence of length n """
dna_list = []
nucleotides = ['A','C','G','T']
for i in range(n):
r = randint(0,3)
dna_list.append(nucleotides[r])
return "".join(dna_list)
def get_complement(c):
""" Returns the complimentary nucleotide to c """
if c == 'A':
return 'T'
if c == 'C':
return 'G'
if c == 'G':
return 'C'
if c == 'T':
return 'A'
def reverse_complement_1(dna):
""" Method 1 for computing the reverse complementary sequence of DNA
for the specfied DNA sequence
dna: a DNA sequence represented as a string
returns: the reverse complementary DNA sequence represented as a string
>>> reverse_complement_1("ATGCCCGCTTT")
'AAAGCGGGCAT'
>>> reverse_complement_1("CCGCGTTCA")
'TGAACGCGG'
"""
return_val = ""
for c in dna:
return_val = get_complement(c) + return_val
return return_val
def reverse_complement_2(dna):
""" Method 2 for Computing the reverse complementary sequence of DNA
for the specfied DNA sequence
dna: a DNA sequence represented as a string
returns: the reverse complementary DNA sequence represented as a string
>>> reverse_complement_2("ATGCCCGCTTT")
'AAAGCGGGCAT'
>>> reverse_complement_2("CCGCGTTCA")
'TGAACGCGG'
"""
return_val = []
for c in reversed(dna):
return_val.append(get_complement(c))
return "".join(return_val)
# Run if called from the command line
if __name__ == "__main__":
# Setup code (if any) to call before test
setup = "from __main__ import generate_random_dna, reverse_complement_1, reverse_complement_2 ; dna = generate_random_dna(2000)"
ntrials = 1000
# Run tests for 1000 executions each
rc1_time = timeit.timeit("reverse_complement_1(dna)", setup=setup, number=ntrials)
print("RC1 time:", rc1_time)
rc2_time = timeit.timeit("reverse_complement_2(dna)", setup=setup, number=ntrials)
print("RC2 time:", rc2_time)