2014年7月26日 星期六

C++ bits fields speed test

C++ bits fields speed test


原始程式碼

https://github.com/poi5305/utility/blob/master/bits_fields_speed_test.cpp


//=========================================//
RT.start_timer("old");
for(uint32_t i=0; i < genome.size()/4; i++)
{
uint8_t cpint ( table[ genome[ (i<<2)+0 ] ] );
for(uint32_t j=1; j<4; j++)
{
cpint = cpint << 2;
cpint = cpint | table[ genome[ (i<<2)+j ]];
}
compressed_genome_2[i] = cpint;
}
RT.print_timer("old");
//=========================================//
//=========================================//
RT.start_timer("bits fields version 1");
for(uint32_t i=0; i < genome.size()/4; i++)
{
compressed_genome_1[i].bits.b4 = table[ genome[ (i<<2)+0 ] ];
compressed_genome_1[i].bits.b3 = table[ genome[ (i<<2)+1 ] ];
compressed_genome_1[i].bits.b2 = table[ genome[ (i<<2)+2 ] ];
compressed_genome_1[i].bits.b1 = table[ genome[ (i<<2)+3 ] ];
}
RT.print_timer("bits fields version 1");
//=========================================//
//=========================================//
RT.start_timer("bits fields version 2");
for(uint32_t i=0; i < genome.size()/4; i++)
{
compressed_genome_3[i].bits = {
table[ genome[ (i<<2)+3 ] ],
table[ genome[ (i<<2)+2 ] ],
table[ genome[ (i<<2)+1 ] ],
table[ genome[ (i<<2)+0 ] ]
};
}
RT.print_timer("bits fields version 2");
//=========================================//

/*
Mac clang -O2
Genome size: 400000000
old: 7315
bits fields version 1: 7258
bits fields version 2: 6582
Values of three methods are the same
bits fields version 3: 6484
Mac clang -O3
Genome size: 400000000
old: 398
bits fields version 1: 779
bits fields version 2: 500
Values of three methods are the same
bits fields version 3: 505
Ubuntu g++-4.8 -O2
Genome size: 400000000
old: 1273
bits fields version 1: 2079
bits fields version 2: 1019
Values of three methods are the same
bits fields version 3: 981
Ubuntu g++-4.8 -O3
Genome size: 400000000
old: 1335
bits fields version 1: 2086
bits fields version 2: 995
Values of three methods are the same
bits fields version 3: 937
*/

沒有留言:

張貼留言