I wrote a simple filter program to see if performance improvement is there with -m64
compiler option over -m32
.
Here is my whole code
#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];
}
}
I compiled it with these two options
g++ -O3 program.c -o filter64 -m64
and
g++ -O3 program.c -o filter32 -m32
Now,
outputs of ./filter32
is
Total time=106807 us
and that of ./filter64
is
Total time=140699 us
My question is shouldn't it be other way ? i.e time taken by filter64 should be less than that of filter32 as with 64 bit architecture we have more registers? How can I achieve that ? or is there any compiler option which takes care of that ? Please help.
I am using ubuntu on intel 64 bit machine.