D
diegotorquemada
Hi all,
As far as I understand, struct padding is implemented in order to speed up the execution of a program. I did this small example below in order to verify this issue. However, surprisingly, my program with struct padding is USUALLY slower than the one with struct packing. Any comments?
Kind regards,
Diego Andrés
#include <stdio.h>
#include <time.h>
#define N 10000000
struct struct_con_padding
{
char a;
int b;
char c;
} x[N];
struct __attribute__((__packed__)) struct_con_packing
{
char a;
int b;
char c;
} y[N];
int main(void)
{
int i;
clock_t tic, toc;
printf("tamaño(x) = %d Mb, tamaño(y) = %d Mb\n", sizeof(x)/1024/1024, sizeof(y)/1024/1024);
tic = clock();
for (i=0; i<N; i++) { x.a = 'Q'; x.b = 1000; x.c = 'R'; }
toc = clock();
printf("Tiempo con padding = %lf segundos\n", (double)(toc-tic)/CLOCKS_PER_SEC);
tic = clock();
for (i=0; i<N; i++) { y.a = 'Q'; y.b = 1000; y.c = 'R'; }
toc = clock();
printf("Tiempo con packing = %lf segundos\n", (double)(toc-tic)/CLOCKS_PER_SEC);
return 0;
}
-------------------------------------------------------------------------------
[daalvarez@earendil ~]$ gcc -Wall -o 07_struct_padding2 07_struct_padding2..c
[daalvarez@earendil ~]$ ./07_struct_padding2
tamaño(x) = 114 Mb, tamaño(y) = 57 Mb
Tiempo con padding = 0.470000 segundos
Tiempo con packing = 0.180000 segundos
[daalvarez@earendil ~]$ gcc --version
gcc (GCC) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[daalvarez@earendil ~]$
As far as I understand, struct padding is implemented in order to speed up the execution of a program. I did this small example below in order to verify this issue. However, surprisingly, my program with struct padding is USUALLY slower than the one with struct packing. Any comments?
Kind regards,
Diego Andrés
#include <stdio.h>
#include <time.h>
#define N 10000000
struct struct_con_padding
{
char a;
int b;
char c;
} x[N];
struct __attribute__((__packed__)) struct_con_packing
{
char a;
int b;
char c;
} y[N];
int main(void)
{
int i;
clock_t tic, toc;
printf("tamaño(x) = %d Mb, tamaño(y) = %d Mb\n", sizeof(x)/1024/1024, sizeof(y)/1024/1024);
tic = clock();
for (i=0; i<N; i++) { x.a = 'Q'; x.b = 1000; x.c = 'R'; }
toc = clock();
printf("Tiempo con padding = %lf segundos\n", (double)(toc-tic)/CLOCKS_PER_SEC);
tic = clock();
for (i=0; i<N; i++) { y.a = 'Q'; y.b = 1000; y.c = 'R'; }
toc = clock();
printf("Tiempo con packing = %lf segundos\n", (double)(toc-tic)/CLOCKS_PER_SEC);
return 0;
}
-------------------------------------------------------------------------------
[daalvarez@earendil ~]$ gcc -Wall -o 07_struct_padding2 07_struct_padding2..c
[daalvarez@earendil ~]$ ./07_struct_padding2
tamaño(x) = 114 Mb, tamaño(y) = 57 Mb
Tiempo con padding = 0.470000 segundos
Tiempo con packing = 0.180000 segundos
[daalvarez@earendil ~]$ gcc --version
gcc (GCC) 4.5.2
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[daalvarez@earendil ~]$