0

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?

C Perfomance
  • 111
  • 3
  • 13
  • "please help me and not close question." , it's off-topic, you'll probably have more luck on the [code review stack exchange](https://codereview.stackexchange.com/). – George Jun 16 '17 at 18:59
  • i just ask a question then how can make this better?! – C Perfomance Jun 16 '17 at 19:01
  • I suspect you're using [Shlemiel's algorithm](http://wiki.c2.com/?ShlemielThePainter), maybe a `strcat()`? – pmg Jun 16 '17 at 19:01
  • 1
    mean use `strcat()` ? so how can append int to char* with `strcat()` with more performance ? – C Perfomance Jun 16 '17 at 19:02
  • 2
    `sizeof(s1)` doesn't do waht you think it does - it returns the size of a pointer,which is not the size of the buffer you need to copy a string into. I'm not even entirely sure what the *intent* of this expression is: `sizeof(s1)+sizeof(s2)+2*sizeof(DATA_VALUE_String *)`. Before you worry about performance too much, make sure the code is correct. – Michael Burr Jun 16 '17 at 19:13
  • @michael-burr , what is better way to `malloc()` `char *`? may say me change to what!? – C Perfomance Jun 16 '17 at 20:30

2 Answers2

0

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.

Sterls
  • 723
  • 12
  • 22
  • tank you . how can append int to string with more performance? – C Perfomance Jun 16 '17 at 19:12
  • I think this function is more efficient than strcat, however, you will need to test if the conversion from int64 to char* will cause it to be worse than the methods you have tried. see: https://stackoverflow.com/questions/9695720/how-do-i-convert-a-64bit-integer-to-a-char-array-and-back – Sterls Jun 16 '17 at 19:15
  • ```#include #include #include #include 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) – C Perfomance Jun 16 '17 at 19:32
  • Q: are you sure then your strConcat() function support utf-8 unicode? – C Perfomance Jun 16 '17 at 20:32
0

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;
}
user803422
  • 2,636
  • 2
  • 18
  • 36