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?