C++ vector 和数组强转::: 效率

在线编程运行链接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, 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值