Bonjour,
Pour avoir une idée des performances de différents langages de programmation sur une petite épreuve portant sur des chaines de caractères j'ai fait passé un petit test répété un très grand nombre de fois.
(...)
Est-ce que tu pourrais nous mettre les sources en C et C++ stp? Je suis surpris par ton classement.
Bonjour,
Pour le C je me sers d'une implémentation que j'ai construite pour les chaines.
Pour le C++ c'est très simple :
using namespace std;
void dotest()
{
string z ( "bArakas mange barakas car boit barakas avec barakas" );
string s1( "ba" );
string s2( "ab") ;
string sb ;
size_t p = 0 ;
//cout << z << endl;
while( 1 )
{
p = z.find( s1 , p );
if( p == string::npos )
break ;
sb = z.substr(p,s1.size()) ;
// cout << "p= " << p << " " << sb << endl ;
p += s1.size();
}
p= 0;
while ( 1 )
{
p = z.find( s1 , p );
if( p == string::npos )
break;
z.replace( p , s1.size(), s2 );
p += s2.size() ;
}
//cout << z << endl;
p= 0;
while ( 1 )
{
p = z.find( s2 , p );
if( p == string::npos )
break;
z.erase( p , s2.size() );
}
//cout << z << endl;
}
Pour le C j'ai extrait la partie nécessaire à ce traitement :
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <ctype.h>
#include <stdarg.h>
#include <assert.h>
#define NOPOS ((size_t) -1)
typedef struct ostring
{
char *g_data;
size_t g_siz;
} ostring;
/************************************************************************************/
char * str_data (ostring * S);
size_t str_size (ostring * S);
char * str_alloc (size_t siz);
void str_init_nb (ostring * S, ...);
void str_clear_nb (ostring * S, ...);
size_t str_find (ostring * S, size_t pos, const char *s, int exact);
void str_clear (ostring * S);
void str_init_with_str (ostring * s, const char *z);
int str_empty (ostring * S);
int str_null (ostring * S);
char * str_substr (ostring * S, ostring * dest, size_t sp, size_t nb);
char * str_delete_at (ostring * S, size_t p, size_t nb);
char * str_insert_at (ostring * S, size_t pos, const char *s);
size_t str_replace_all (ostring * S, size_t p ,const char *oc, const char *nc, int exact);
size_t str_remove_all (ostring * S, size_t p ,const char *z, int exact);
/**********************************************************************/
char *str_data (ostring * S)
{
return S->g_data;
}
size_t str_size (ostring * S)
{
return S->g_siz;
}
char *str_alloc (size_t siz)
{
char *el = 0;
if (siz > 0)
{
el = (char *) malloc (siz * sizeof (char));
assert (el != 0);
}
return el;
}
void str_init_nb (ostring * S, ...)
{
va_list args;
va_start (args, S);
while (S != NULL)
{
S->g_siz = 0;
S->g_data = 0;
S = va_arg (args, ostring *);
}
va_end (args);
}
void str_clear_nb (ostring * S, ...)
{
va_list args;
va_start (args, S);
while (S != NULL)
{
if (S->g_data)
{
free (S->g_data);
S->g_data = 0;
S->g_siz = 0;
}
S = va_arg (args, ostring *);
}
va_end (args);
}
size_t str_find (ostring * S, size_t pos, const char *s, int exact)
{
if (!s)
return NOPOS;
if (pos > S->g_siz)
return NOPOS;
const char* ptr = 0 ;
const char *z = S->g_data + pos;
if( exact )
ptr = strstr( z , s );
if( ! exact )
ptr = strcasestr( z , s );
if (ptr != 0)
return ptr - S->g_data;
return NOPOS;
}
void str_clear (ostring * S)
{
if (S->g_siz > 0)
{
free (S->g_data);
S->g_siz = 0;
S->g_data = 0;
}
}
void str_init_with_str (ostring * s, const char *z)
{
if (z == 0)
z = "";
s->g_siz = strlen (z);
s->g_data = str_alloc (s->g_siz + 1);
strcpy (s->g_data, z);
}
int str_empty (ostring * S)
{
return str_size (S) == 0;
}
int str_null (ostring * S)
{
return (S->g_data == 0) && (S->g_siz == 0);
}
char *str_substr (ostring * S, ostring * dest, size_t sp, size_t nb)
{
assert( 0 == str_null(S) );
if (sp > S->g_siz || str_empty (S))
return S->g_data;
nb = (sp + nb) > str_size (S) ? str_size (S) - sp : nb;
if( nb != dest->g_siz )
{
str_clear(dest);
dest->g_data = str_alloc (nb + 1);
strncpy (&dest->g_data[0], &S->g_data[sp], nb);
*(dest->g_data + nb) = 0;
}
else
{
strncpy (&dest->g_data[0], &S->g_data[sp], nb);
}
dest->g_siz = nb;
return dest->g_data;
}
char *str_delete_at (ostring * S, size_t p, size_t nb)
{
if (S->g_siz == 0 || p >= S->g_siz)
return S->g_data;
nb = (p + nb > S->g_siz) ? S->g_siz - p : nb;
memmove (S->g_data + p, S->g_data + (p + nb), S->g_siz - (p + nb));
S->g_siz -= nb;
*(S->g_data + S->g_siz) = 0;
return S->g_data;
}
char *str_insert_at (ostring * S, size_t pos, const char *s)
{
assert (0 == str_null (S));
size_t osiz = S->g_siz;
size_t sublen = strlen (s);
pos = (pos > osiz) ? osiz : pos;
char *az = str_alloc ((S->g_siz += sublen) + 1);
char *z = az;
char *s1 = S->g_data;
while (pos--)
*z++ = *s1++;
while (*s)
*z++ = *s++;
while (*s1)
*z++ = *s1++;
*z = 0;
free (S->g_data);
S->g_data = az;
return S->g_data;
}
size_t str_replace_all (ostring * S, size_t p ,const char *oc, const char *nc,
int exact)
{
size_t nb = 0;
size_t len = strlen (oc);
size_t n = strlen(nc);
while (NOPOS != (p = str_find (S, p, oc, exact)))
{
str_delete_at (S, p, len);
str_insert_at (S, p, nc);
nb += 1;
p += n ;
}
return nb;
}
size_t str_remove_all (ostring * S, size_t p ,const char *z, int exact)
{
size_t nb = 0;
if (!z)
return nb;
size_t len = strlen (z);
while (NOPOS != (p = str_find (S, p, z, exact)))
{
str_delete_at (S, p, len);
nb += 1;
}
return nb;
}
/*******************************************************************************************/
/* c'est ici le test */
void dotest()
{
ostring z1, z2 ;
str_init_nb (&z1, &z2, NULL);
size_t p = 0 ;
str_init_with_str (&z1, "bArakas mange barakas car boit barakas avec barakas");
//puts (str_data (&z1));
const char * motif = "ba";
size_t len = strlen( motif );
while(1)
{
p = str_find( &z1, p , motif , 1);
if( p == NOPOS )
break ;
char * s = str_substr( &z1, &z2 , p, len );
p += len ;
}
str_replace_all( &z1, 0, "ba" , "ab" , 1 );
//puts (str_data (&z1));
str_remove_all( &z1, 0, "ab" , 1 );
//puts (str_data (&z1));
str_clear_nb (&z1, &z2, NULL);
}
j'ai mesuré sur 9 000 000 répétions.
Cordialement.