简体   繁体   中英

What is Best way concatenate strings and number - Performance Using C?

Please go down and read new/last update section.

i very try to write a code with good performance.

but also php interpreter script is more fast of my c app .

i am test this in a big loop. and i sure then speed of my concatenate code is bad. and sure then can make this better like php script.


Complate Source(c):

for(int count=1;count<=1000000;count++)
{
    results=str_int("New Item",count);
}

str_int(...) Function :

#1 :

DATA_VALUE_String *str_int(DATA_VALUE_String *s1,DATA_VALUE_Int64 s2)
{
    DATA_VALUE_String *result=malloc(sizeof(s1)+sizeof(s2)+2*sizeof(DATA_VALUE_String *));
    snprintf(result,sizeof(s2)+sizeof(s2),"%s%d",s1,s2);
    return result;
}

Time : 0m0.135s

#2 :

DATA_VALUE_String *str_int(DATA_VALUE_String *s1,DATA_VALUE_Int64 s2)
{
    DATA_VALUE_String *result=malloc(sizeof(s1)+sizeof(s2)+2*sizeof(DATA_VALUE_String *)); 
    DATA_VALUE_String *ss2;
    ss2=malloc((sizeof(s2)+2)*sizeof(DATA_VALUE_String *));
    sprintf(ss2,"%"PRId64,s2);
    strcat(strcpy(result,s1),ss2);
    return result;
}

Time : 0m0.160s


But Php 7.1.4 : 0.081s

<?php
//$myArrays = [];
for($count=1;$count<=1000000;$count++)
{
    $results="";
    $results="New Item".$count;
}
//unset($myArrays);
?>

please help me to make this c file more fast...

i want make my c code better.

php have more performance in concatenate string,int. but my c code is not like them.

how can make this better?

tank you very much. :like:

=============

New Update for Answer 1:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

void int64ToChar(char **mesg, int64_t num) {
    //*mesg="..";
    *(int64_t *)mesg = num;
}
int main()
{
    int64_t num=4694;
    char *nums=malloc(6*sizeof(char *));
    int64ToChar(&nums,num);
    printf("%s",nums);
    return 0;
}

Error : Segmentation fault (core dumped)


New/Last Update for Bad Performance (C vs PHP)

php(last version) : http://codepad.org/9D26wLEA

$ time php arrays2.php
real    0m0.089s
user    0m0.086s
sys 0m0.004s

c : http://codepad.org/JmemaXOr

$ gcc arrays.c -o arrays -O3 -w
$ time ./arrays
real    0m0.131s
user    0m0.091s
sys 0m0.040s

How can make my C file better?

You can try to concatenate strings in C by directly adding the second string to the end of the first string in memory via pointers.

 char* strConcat(char* str1,char* str2){
     while (*str1) str1++;
     do{
        *str1++ = *str2++
     }while (*str2); 
     return --str1; //return a pointer to the end of the new string
 }

This returns a pointer to the end of the new concatenated string so you can just pass the pointer along to continue to concatenate to this current string. Alternately, if no further concatenation is necessary, you can maintain the pointer to the head of the concatenated string.

Someone gave an algorithm much faster than snprintf for converting an int to a string : How to convert an int to string in C

This algo (I named it xitoa below) is also faster than the PHP script. (I tested with int32, rather than int64, but it illustrates a significant improvement over snprintf)

My benchmark:

  • with snprintf: 1.54s
  • with xitoa: 0.99s
  • with PHP: 1.23s

These results were obtained with gcc optimization -O2 (for snprintf and xitoa).

Here is the algo that I tested (copied from the given link):

char * xitoa (int value, char *buffer, int base)
{
    // check that the base if valid
    if (base < 2 || base > 36) { *buffer = '\0'; return buffer; }

    char* ptr = buffer, *ptr1 = buffer, tmp_char;
    int tmp_value;

    do {
        tmp_value = value;
        value /= base;
        *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
    } while ( value );

    // Apply negative sign
    if (tmp_value < 0) *ptr++ = '-';
    *ptr-- = '\0';

    // reverse the characters, as they were stored less-significant first
    while (ptr1 < ptr) {
        tmp_char = *ptr;
        *ptr--= *ptr1;
        *ptr1++ = tmp_char;
    }
    return buffer;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM