【发布时间】:2015-02-02 10:39:23
【问题描述】:
我编写了一个简单的过滤器程序来查看使用-m64 编译器选项是否比-m32 有性能改进。
这是我的全部代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/time.h>
#define __STDC_FORMAT_MACROS 1
#include<inttypes.h>
#define tap_size 5
int luma_stride=640;
int luma_ht=480;
int croma_stride=320;
int croma_ht=240;
int filter[tap_size]={-3,2,3,2,-3};
struct timeval tv1, tv2,tv3;
uint64_t ui1;
uint64_t total_time=0;
uint64_t GetTimeStamp();
void process_frame(unsigned char *ip_buffer, unsigned char * op_buffer, int ip_buf_size, int op_buf_size);
int main()
{
int ip_buf_size;
int op_buf_size;
unsigned char * ip_buffer;
unsigned char * op_buffer;
unsigned char * temp;
ip_buf_size=luma_stride*luma_ht + 2*croma_stride * croma_ht;
op_buf_size=ip_buf_size; //
ip_buffer = (unsigned char *)malloc(ip_buf_size*sizeof(char));
op_buffer = (unsigned char *)malloc(ip_buf_size*sizeof(char));;
temp=ip_buffer;
for(int i=0;i<ip_buf_size;i++)
{
*temp=rand();
}
for(int i=0;i<100;i++)
{
ui1=GetTimeStamp();
process_frame(ip_buffer, op_buffer, ip_buf_size, op_buf_size);//process
total_time+=GetTimeStamp()-ui1;
}
free(ip_buffer);
free(op_buffer);
printf("\nTotal time=%" PRIu64 " us\n", total_time);
return 0;
}
uint64_t GetTimeStamp()
{
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
}
void process_frame(unsigned char *ip_buffer, unsigned char * op_buffer, int ip_buf_size, int op_buf_size)
{
int i,j;
unsigned char *ptr1,*ptr2;
unsigned char *temp_buffer=(unsigned char *) malloc(op_buf_size*sizeof(unsigned char));
ptr1=ip_buffer;
//ptr2=temp_buffer;
ptr2=op_buffer;
//Vertical filter
//Luma
/* for(j=0;j<tap_size/2;j++)
{
for(i=0;i<luma_stride;i++)
{
*ptr2++=*ptr1++;
}
} */
memcpy(ptr2,ptr1,2*luma_stride*sizeof(unsigned char));
ptr1=ip_buffer+2*luma_stride;
ptr2=op_buffer+2*luma_stride;
for(i=0;i<luma_ht-tap_size+1;i++)
{
for(j=0;j<luma_stride;j++)
{
int k;
long int temp=0;
for(k=0;k<tap_size;k++)
{
temp+=filter[k]**(ptr1+(k-tap_size/2)*luma_stride);
}
//temp=temp>>4;
if(temp>255) temp =255;
else if(temp<0) temp=0;
*ptr2=temp;
++ptr1;
++ptr2;
}
}
memcpy(ptr2,ptr1,2*luma_stride*sizeof(unsigned char));
ptr1=ptr1+2*luma_stride;
ptr2=ptr2+2*luma_stride;
//Copy croma values as it is!
for(i=luma_ht*luma_stride;i<ip_buf_size;i++)
{
op_buffer[i]=ip_buffer[i];
}
}
我用这两个选项编译它
g++ -O3 program.c -o filter64 -m64
和
g++ -O3 program.c -o filter32 -m32
现在,
./filter32 的输出是
Total time=106807 us
而./filter64的那个是
Total time=140699 us
我的问题是不应该是其他方式吗?即 filter64 所花费的时间应该少于 filter32 所花费的时间,因为使用 64 位架构我们有更多的寄存器?我怎样才能做到这一点?或者是否有任何编译器选项可以解决这个问题? 请帮忙。
我在英特尔 64 位机器上使用 ubuntu。
【问题讨论】:
-
如果只测量时间,在 for 循环之前和之后(而不是 for 内的 100 次),结果是否相同?如果将重复次数从 100 增加到例如 100000,结果是否相同?
-
大量使用指针的代码可能会降低性能,因为现在可以放入缓存的指针减少了。您还应该内联
GetTimeStamp并尽可能使用更高精度的计时器。另一个解决方案是-mx32
标签: c performance compiler-optimization