[omniORB-dev] CORBA::string_dup

Serguei Kolos Serguei.Kolos at cern.ch
Mon Oct 6 19:32:08 BST 2003


Skipped content of type multipart/alternative-------------- next part --------------
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

using namespace std;

#define SIZE 1024

char * my_strdup( const char * s )
{
	register const char *p = s;
	for ( ;*p++; );
	
	char * d = new char[p-s+1];
	
	p = s;
	register char * r = d;
	for ( ;*p++; ) 
		*r++ = *p++;
	*r = 0;
	return d;
}

char * my_strdup1( const char * s )
{
	int i = strlen( s );
	
	char * d = new char[i+1];
	
	strcpy( d, s );
	
	return d;
}

char * my_strdup2( const char * s )
{
	const char *p = s;
	for ( ;*p++; );
	
	char * d = new char[p-s+1];
	
	memcpy( d, s, p-s+1 );
	return d;
}

char * my_strdup3( const char * s )
{
	int i = strlen( s );
	
	char * d = new char[i+1];
	
	memcpy( d, s, i+1 );
	return d;
}

void my_strdup4( char * d, const char * s )
{
	strcpy( d, s );
}

void my_strdup5( char * d, const char * s )
{
	memcpy( d, s, SIZE );
}


int main()
{
	struct timeval start, stop;
	int i;
	float time;
	
	char buff[SIZE];
	memset( buff, '1', SIZE-1 );
	buff[SIZE-1] = 0;
		
	gettimeofday( &start, 0 );
	for ( i = 0; i < 1000000; i++ )
	{
		char * d = strdup( buff );
		free( d );
		buff[24] = '2';
	}
	gettimeofday( &stop, 0 );
	
	time = ( stop.tv_sec - start.tv_sec ) * 1000000 + ( stop.tv_usec - start.tv_usec );
	cout << "system strdup " << time/1000000. << endl;
	
	gettimeofday( &start, 0 );
	for ( i = 0; i < 1000000; i++ )
	{
		char * d = my_strdup( buff );
		free( d );
	}
	gettimeofday( &stop, 0 );
	
	time = ( stop.tv_sec - start.tv_sec ) * 1000000 + ( stop.tv_usec - start.tv_usec );
	cout << "my_strdup (for loop for copy and length) " << time/1000000. << endl;
	
	gettimeofday( &start, 0 );
	for ( i = 0; i < 1000000; i++ )
	{
		char * d = my_strdup1( buff );
		delete[] d;
	}
	gettimeofday( &stop, 0 );
	
	time = ( stop.tv_sec - start.tv_sec ) * 1000000 + ( stop.tv_usec - start.tv_usec );
	cout << "my_strdup1 (strlen + strcpy) " << time/1000000. << endl;
	
	gettimeofday( &start, 0 );
	for ( i = 0; i < 1000000; i++ )
	{
		char * d = my_strdup2( buff );
		delete[] d;
	}
	gettimeofday( &stop, 0 );
	
	time = ( stop.tv_sec - start.tv_sec ) * 1000000 + ( stop.tv_usec - start.tv_usec );
	cout << "my_strdup2 (for loop for length + memcpy) " << time/1000000. << endl;
	
	gettimeofday( &start, 0 );
	for ( i = 0; i < 1000000; i++ )
	{
		char * d = my_strdup3( buff );
		delete[] d;
	}
	gettimeofday( &stop, 0 );
	
	time = ( stop.tv_sec - start.tv_sec ) * 1000000 + ( stop.tv_usec - start.tv_usec );
	cout << "my_strdup3 (strlen + memcpy) " << time/1000000. << endl;
	
	gettimeofday( &start, 0 );
	char * d = new char[strlen(buff)+1];
	for ( i = 0; i < 1000000; i++ )
	{
		my_strdup4( d, buff );
	}
	gettimeofday( &stop, 0 );
	
	time = ( stop.tv_sec - start.tv_sec ) * 1000000 + ( stop.tv_usec - start.tv_usec );
	cout << "strcpy only " << time/1000000. << endl;
	
	gettimeofday( &start, 0 );
	for ( i = 0; i < 1000000; i++ )
	{
		my_strdup5( d, buff );
	}
	gettimeofday( &stop, 0 );
	
	time = ( stop.tv_sec - start.tv_sec ) * 1000000 + ( stop.tv_usec - start.tv_usec );
	cout << "memcpy only " << time/1000000. << endl;
	
}


More information about the omniORB-dev mailing list