Kürzlich begann ich ein Programm zum Vergleich der DNA-Sequenz zu schreiben. Da das Alphabet nur aus vier Buchstaben (ATCG) besteht, schien es bei einem Komprimieren jedes Zeichens auf 2 Bits schneller zu sein (zwei gleiche oder unterschiedliche Zeichen). Wenn ich jedoch einen Test lief, waren Char-Vergleiche viel schneller als Bit-Vergleiche (um ~ 30%). Die Kompression wurde in beiden Programmen als Kontrolle durchgeführt. Was fehlt mir hier? Gibt es eine effizientere Möglichkeit, Bits zu vergleichen? p.s. Ich habe auch versucht Vektor, aber es war ein bisschen langsamer als Bitset.String-Komprimierung und Vergleich
// File: bittest.cc
// Test use of bitset container
#include <ctime>
#include <iostream>
#include <bitset>
#include <vector>
#include <string>
using namespace std;
void compress(string&, bitset<74>&);
void compare(bitset<74>&, bitset<74>&);
int main()
{
// Start timer
std::clock_t start;
double difference;
start = std::clock();
for(int i=0; i<10000000; ++i){
string frag1="ATCGACTGACTGACTGACTGACTGACTGACTGACTGA";
string frag2="AACGAACGAACGAACGAACGAACGAACGAACGAACGA";
int a=37;
bitset<74> bits1;
bitset<74> bits2;
compress(frag1, bits1);
compress(frag2, bits2);
compare(bits1, bits2);
}
difference = (std::clock() - start)/(double)CLOCKS_PER_SEC;
int minutes = difference/60;
int seconds = difference - minutes * 60;
if (seconds < 10){
cout << "\nRunning time: " << minutes << ":0" << seconds << endl << endl;
}else{
cout << "\nRunning time: " << minutes << ":" << seconds << endl << endl;
}
return 0;
}
void compress(string& in, bitset<74>& out){
char c;
int b=0;
for(int i=0; i<in.length(); ++i){
c=in[i];
b=2*i;
switch(c){
case 'A':
break;
case 'C':
out.set(b+1);
break;
case 'G':
out.set(b);
break;
case 'T':
out.set(b);
out.set(b+1);
break;
default:
cout << "Invalid character in fragment.\n";
}
}
}
void compare(bitset<74>& a, bitset<74>& b){
for(int i=0; i<74; ++i){
if(a[i] != b[i]){
}
}
}
und die String-Gurtzeug ...
// File: bittest.cc
#include <ctime>
#include <iostream>
#include <bitset>
#include <vector>
#include <string>
using namespace std;
void compress(string&, bitset<74>&);
void compare(string&, string&);
int main()
{
// Start timer
std::clock_t start;
double difference;
start = std::clock();
for(int i=0; i<10000000; ++i){
string frag1="ATCGACTGACTGACTGACTGACTGACTGACTGACTGA";
string frag2="AACGAACGAACGAACGAACGAACGAACGAACGAACGA";
int a=37;
bitset<74> bits1;
bitset<74> bits2;
compress(frag1, bits1);
compress(frag2, bits2);
compare(frag1, frag2);
}
difference = (std::clock() - start)/(double)CLOCKS_PER_SEC;
int minutes = difference/60;
int seconds = difference - minutes * 60;
if (seconds < 10){
cout << "\nRunning time: " << minutes << ":0" << seconds << endl << endl;
}else{
cout << "\nRunning time: " << minutes << ":" << seconds << endl << endl;
}
return 0;
}
void compress(string& in, bitset<74>& out){
char c;
int b=0;
for(int i=0; i<in.length(); ++i){
c=in[i];
b=2*i;
switch(c){
case 'A':
break;
case 'C':
out.set(b+1);
break;
case 'G':
out.set(b);
break;
case 'T':
out.set(b);
out.set(b+1);
break;
default:
cout << "Invalid character in frag.\n";
}
}
}
void compare(string& a, string& b){
for(int i=0; i<37; ++i){
if(a[i] != b[i]){
}
}
}
'std :: Vektor' sollte nicht einmal existieren –
calccrypto
Welche Compiler-Optionen verwenden Sie? Ich würde einen optimierenden Compiler erwarten, um beide 'compare()' Funktionen vollständig zu entfernen, da sie keinen tatsächlichen Effekt haben. – Blastfurnace
@Blastofen: g ++. Ich habe die compare() -Funktionen etwas tun lassen und die Zeiten sind gleich, also glaube ich, dass g ++ sie nicht entfernt hat. – vincent