Assembly optimizations

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I compiled the following code with gcc-3.3.1. Once without
optimization and one time with optimization -O. With optimization the
Decrement function takes a LOT longer than before whereas the
Increment function stays constant. I haven't found anything in the
manpage about it ... Any help is appreciated.

cheers,
Klaus

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int g_Lock_Mini = 0;

inline int Increment( int* count)
{
	__asm__ __volatile__( "movl $1, %%eax\n\t" 
			      "lock; xaddl %%eax, (%%ecx)\n\t"
			      "incl %%eax\n\t" : : "c" (count) );
}
inline int Decrement( int * count)
{
	__asm__ __volatile__( "lock; decl (%%ecx)\n\t"
				  "movl (%%ecx), %%eax\n\t" : : "c" (count));
}

int Timeval_Substract (struct timeval *result, struct timeval* x,
struct timeval* y)
     {
       /* Perform the carry for the later subtraction by updating y. */
       if (x->tv_usec < y->tv_usec) {
         int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
         y->tv_usec -= 1000000 * nsec;
         y->tv_sec += nsec;
       }
       if (x->tv_usec - y->tv_usec > 1000000) {
         int nsec = (x->tv_usec - y->tv_usec) / 1000000;
         y->tv_usec += 1000000 * nsec;
         y->tv_sec -= nsec;
       }
     
       /* Compute the time remaining to wait.
          tv_usec is certainly positive. */
       result->tv_sec = x->tv_sec - y->tv_sec;
       result->tv_usec = x->tv_usec - y->tv_usec;
     
       /* Return 1 if result is negative. */
       return x->tv_sec < y->tv_sec;
}

int main( void )
{
	int * inc;
	*inc = 0;	
	int i;
	struct timeval startTime, endTime, resultTime;

	gettimeofday( &startTime, NULL );
	for( i = 0; i < 40000000; i++ )
		Increment( inc );
	gettimeofday( &endTime, NULL );
	Timeval_Substract( &resultTime, &endTime, &startTime );
	printf( "Increment Used Time: %i sec, %i nanosec\n",
resultTime.tv_sec, resultTime.tv_usec);
	
	gettimeofday( &startTime, NULL );
	for( i = 0; i < 40000000; i++ )
		Decrement( inc );
	gettimeofday( &endTime, NULL );
	Timeval_Substract( &resultTime, &endTime, &startTime );
	printf( "Decrement Used Time: %i sec, %i nanosec\n",
resultTime.tv_sec, resultTime.tv_usec);

    return 2;
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux