在线编程运行链接1:https://c.runoob.com/compile/12
在线编程运行链接2:http://www.dooccn.com/cpp/
#include <vector>
#include <iostream>
#include <stdio.h>
using std::vector;
unsigned long get_cycle_count()
{
unsigned int lo,hi;
asm volatile ("rdtsc":"=a"(lo),"=d"(hi));// 64bit
return (((unsigned long)hi<<32)|lo);
}
/*
unsigned long ulStartCycle = 0;
unsigned long ulendCycle=0;
for(int j =0; j<20 ;j++)
{
ulStartCycle = get_cycle_count();
// memcmp((char*)&da,(char*)&data[j*16],16);
memcpy((char*)&da,(char*)&data[j*16],32);
ulendCycle=get_cycle_count();
}
*/
int main(void){
unsigned long ulStartCycle = 0;
unsigned long ulendCycle=0;
int ary[] = {10, 18, 9, 1, 66, 0, 102, 32, 99, 70};
#if 1
ulStartCycle = get_cycle_count();
vector<int> vec(ary, ary + sizeof(ary) / sizeof(int));
ulendCycle=get_cycle_count();
printf("memcmp count cycle = %lu\n", (ulendCycle -ulStartCycle));
#else
ulStartCycle = get_cycle_count();
std::vector<int> vec;
int len = sizeof(ary) / sizeof(int);
vec.resize(len);
copy(ary, ary + len, vec.begin());
ulendCycle=get_cycle_count();
printf("memcmp count cycle = %lu\n", (ulendCycle -ulStartCycle));
#endif
for(int i = 0; i != vec.size(); ++i){
std::cout << vec[i] << ", ";
}
std::cout << std::endl;
return 0;
}
结果: #if 1
memcmp count cycle = 4641
10, 18, 9, 1, 66, 0, 102, 32, 99, 70,
#if 0
memcmp count cycle = 7024
10, 18, 9, 1, 66, 0, 102, 32, 99, 70,
总结: 在运行时定义vector 会反复申请内存,效率并不理想
参考:
http://bcoder.com/others/convert-between-vector-and-array-in-cplusplus
https://blog.csdn.net/sandalphon4869/article/details/98452007
https://blog.csdn.net/sandalphon4869/article/details/98452007
https://blog.csdn.net/qq_43066253/article/details/88702460
测试2:在线编程运行链接2:http://www.dooccn.com/cpp/
#include <vector>
#include <iostream>
#include <stdio.h>
#include <string.h>
using std::vector;
unsigned long get_cycle_count()
{
unsigned int lo,hi;
asm volatile ("rdtsc":"=a"(lo),"=d"(hi));// 64bit
return (((unsigned long)hi<<32)|lo);
}
/*
unsigned long ulStartCycle = 0;
unsigned long ulendCycle=0;
for(int j =0; j<20 ;j++)
{
ulStartCycle = get_cycle_count();
// memcmp((char*)&da,(char*)&data[j*16],16);
memcpy((char*)&da,(char*)&data[j*16],32);
ulendCycle=get_cycle_count();
}
*/
int main(void){
unsigned long ulStartCycle = 0;
unsigned long ulendCycle=0;
int ary[] = {10, 18, 9, 1, 66, 0, 102, 32, 99, 70};
int b[10]={0};
ulStartCycle = get_cycle_count();
memcpy((void *)&b,(void *)&ary[0],sizeof(ary));
ulendCycle=get_cycle_count();
printf("memcmp count cycle = %lu\n", (ulendCycle -ulStartCycle));
for(int i = 0; i < 10; ++i){
std::cout << b[i] << ", ";
}
std::cout << std::endl;
#if 0
ulStartCycle = get_cycle_count();
vector<int> vec(ary, ary + sizeof(ary) / sizeof(int));
ulendCycle=get_cycle_count();
printf("memcmp1 count cycle = %lu\n", (ulendCycle -ulStartCycle));
#else
ulStartCycle = get_cycle_count();
std::vector<int> vec;
int len = sizeof(ary) / sizeof(int);
vec.resize(len);
copy(ary, ary + len, vec.begin());
ulendCycle=get_cycle_count();
printf("memcmp2 count cycle = %lu\n", (ulendCycle -ulStartCycle));
#endif
for(int i = 0; i != vec.size(); ++i){
std::cout << vec[i] << ", ";
}
std::cout << std::endl;
return 0;
}
#if 0
memcmp count cycle = 55
10, 18, 9, 1, 66, 0, 102, 32, 99, 70,
memcmp2 count cycle = 8803
10, 18, 9, 1, 66, 0, 102, 32, 99, 70,
#if 1
memcmp count cycle = 44
10, 18, 9, 1, 66, 0, 102, 32, 99, 70,
memcmp2 count cycle = 10136
10, 18, 9, 1, 66, 0, 102, 32, 99, 70,