Skip to content

Instantly share code, notes, and snippets.

@dkinzer
Last active August 29, 2015 14:16
Show Gist options
  • Save dkinzer/0530e2ed6c27b19462b0 to your computer and use it in GitHub Desktop.
Save dkinzer/0530e2ed6c27b19462b0 to your computer and use it in GitHub Desktop.
Bench marking some Complement class implementations.
user system total real
Complement_V0 with 1 Nucleotides 1.300000 0.000000 1.300000 ( 1.294155)
Complement_V0 with 2 Nucleotides 1.770000 0.000000 1.770000 ( 1.769186)
Complement_V0 with 4 Nucleotides 2.920000 0.000000 2.920000 ( 2.941324)
Complement_V0 with 8 Nucleotides 4.500000 0.000000 4.500000 ( 4.510645)
Complement_V0 with 10 Nucleotides 5.210000 0.000000 5.210000 ( 5.254469)
Complement_V1 with 1 Nucleotides 6.150000 0.010000 6.160000 ( 6.169194)
Complement_V1 with 2 Nucleotides 6.710000 0.010000 6.720000 ( 6.764511)
Complement_V1 with 4 Nucleotides 7.590000 0.000000 7.590000 ( 7.625991)
Complement_V1 with 8 Nucleotides 10.200000 0.000000 10.200000 ( 10.236003)
Complement_V1 with 10 Nucleotides 10.520000 0.030000 10.550000 ( 10.592688)
Complement_V2 with 1 Nucleotides 1.970000 0.000000 1.970000 ( 1.973776)
Complement_V2 with 2 Nucleotides 2.460000 0.000000 2.460000 ( 2.462186)
Complement_V2 with 4 Nucleotides 3.400000 0.000000 3.400000 ( 3.406930)
Complement_V2 with 8 Nucleotides 5.110000 0.000000 5.110000 ( 5.123110)
Complement_V2 with 10 Nucleotides 5.700000 0.000000 5.700000 ( 5.728795)
Complement_V3 with 1 Nucleotides 5.110000 0.000000 5.110000 ( 5.122803)
Complement_V3 with 2 Nucleotides 6.770000 0.000000 6.770000 ( 6.782132)
Complement_V3 with 4 Nucleotides 9.470000 0.020000 9.490000 ( 9.501953)
Complement_V3 with 8 Nucleotides 16.440000 0.010000 16.450000 ( 16.560662)
Complement_V3 with 10 Nucleotides 18.930000 0.010000 18.940000 ( 18.985363)
Complement_V4 with 1 Nucleotides 5.140000 0.000000 5.140000 ( 5.141769)
Complement_V4 with 2 Nucleotides 6.420000 0.000000 6.420000 ( 6.430745)
Complement_V4 with 4 Nucleotides 9.390000 0.000000 9.390000 ( 9.393651)
Complement_V4 with 8 Nucleotides 15.120000 0.020000 15.140000 ( 15.200267)
Complement_V4 with 10 Nucleotides 17.910000 0.000000 17.910000 ( 17.975538)
require 'benchmark'
class Complement_V0
DNA_RNA_COMPLEMENTS = {
'G' => 'C',
'C' => 'G',
'T' => 'A',
'A' => 'U',
}
RNA_DNA_COMPLEMENTS = {
'G' => 'C',
'C' => 'G',
'T' => 'A',
'U' => 'A',
'A' => 'T',
}
class << self
def of_dna dna_string
dna = dna_string.chars
transcribe dna, DNA_RNA_COMPLEMENTS
end
def of_rna rna_string
rna = rna_string.chars
transcribe rna, RNA_DNA_COMPLEMENTS
end
private
def transcribe nucleotides, complements
nucleotides.map{ |nucleotide| complements[nucleotide] }.reduce(:+)
end
end
end
class Complement_V1
DNA_NUCLEOTIDES = 'GCTA'
RNA_COMPLEMENTS = { 'G' => 'C', 'C' => 'G', 'T' => 'A', 'A' => 'U' }
RNA_NUCLEOTIDES = 'CGAU'
DNA_COMPLEMENTS = { 'C' => 'G', 'G' => 'C', 'A' => 'T', 'U' => 'A' }
class << self
def of_dna dna
dna.gsub(/[#{DNA_NUCLEOTIDES}]/, RNA_COMPLEMENTS)
end
def of_rna rna
rna.gsub(/[#{RNA_NUCLEOTIDES}]/, DNA_COMPLEMENTS)
end
end
end
class Complement_V2
RNA_COMPLEMENTS = { 'G' => 'C', 'C' => 'G', 'T' => 'A', 'A' => 'U' }
DNA_COMPLEMENTS = { 'C' => 'G', 'G' => 'C', 'A' => 'T', 'U' => 'A' }
class << self
def of_dna dna
dna.gsub(/[GCTA]/, RNA_COMPLEMENTS)
end
def of_rna rna
rna.gsub(/[CGAU]/, DNA_COMPLEMENTS)
end
end
end
class Complement_V3
NUCLEOTIDE_COMPLEMENTS = { 'G' => 'C', 'C' => 'G', 'T' => 'A', 'A' => 'U' }
class << self
def of_dna strand
DNA.new(strand).gsub(/[GCTA]/, NUCLEOTIDE_COMPLEMENTS)
end
def of_rna strand
RNA.new(stran).gsub(/[CGAU]/, NUCLEOTIDE_COMPLEMENTS.invert)
end
end
end
class Complement_V4
RNA_COMPLEMENTS = { 'G' => 'C', 'C' => 'G', 'T' => 'A', 'A' => 'U' }
DNA_COMPLEMENTS = { 'C' => 'G', 'G' => 'C', 'A' => 'T', 'U' => 'A' }
class << self
def of_dna strand
DNA.new(strand).gsub(/[GCTA]/, RNA_COMPLEMENTS)
end
def of_rna strand
RNA.new(strand).gsub(/[CGAU]/, DNA_COMPLEMENTS)
end
end
end
class DNA < String
def initialize strand
strand.chars.map do |n|
throw "Illegal Nucleotide: #{n}" unless ['G', 'C', 'T', 'A'].include? n
end
super
end
end
class RNA < String
def initialize strand
strand.chars.map do |n|
throw "Illegal Nucleotide: #{n}" unless ['G', 'C', 'U', 'A'].include? n
end
super
end
end
ITERATIONS = 1000000
def run(complement, dna, bench)
bench.report("#{complement.name} with #{dna.length} Nucleotides") do
ITERATIONS.times do
complement.of_dna dna
end
end
end
Benchmark.bm do |bench|
run(Complement_V0, "G", bench)
run(Complement_V0, "GC", bench)
run(Complement_V0, "GCCG", bench)
run(Complement_V0, "GCCGGCCG", bench)
run(Complement_V0, "ATTAGGGCAT", bench)
puts ""
run(Complement_V1, "G", bench)
run(Complement_V1, "GC", bench)
run(Complement_V1, "GCCG", bench)
run(Complement_V1, "GCCGGCCG", bench)
run(Complement_V1, "ATTAGGGCAX", bench)
puts ""
run(Complement_V2, "G", bench)
run(Complement_V2, "GC", bench)
run(Complement_V2, "GCCG", bench)
run(Complement_V2, "GCCGGCCG", bench)
run(Complement_V2, "ATTAGGGCAX", bench)
puts ""
run(Complement_V3, "G", bench)
run(Complement_V3, "GC", bench)
run(Complement_V3, "GCCG", bench)
run(Complement_V3, "GCCGGCCG", bench)
run(Complement_V3, "ATTAGGGCAT", bench)
puts ""
run(Complement_V4, "G", bench)
run(Complement_V4, "GC", bench)
run(Complement_V4, "GCCG", bench)
run(Complement_V4, "GCCGGCCG", bench)
run(Complement_V4, "ATTAGGGCAT", bench)
puts ""
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment