什么是连接字符串和数字的最佳方法 - 使用C的性能?

时间:2017-06-16 18:57:32

标签: c string performance

请继续阅读new/last update部分。

我非常努力编写性能良好的代码。

php interpreter script的{​​{1}}速度更快。

我在一个大循环中测试它。我确定我的连接代码的速度很糟糕。 并确定然后可以像PHP脚本一样更好。

Complate Source(c):

c app

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

#1:

str_int(...)

时间: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; }

#2:

0m0.135s

时间: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; }

但是Php 7.1.4:0m0.160s

0.081s

请帮助我更快地制作此c文件...

我想让我的c代码更好。

php在连接字符串int中有更多性能。 但我的c代码与他们不一样。

怎样才能让这更好?

非常坦克。 :这样的:

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

答案1的新更新:

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

错误:#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; }

性能不佳的新/上次更新(C与PHP)

php(最新版本):http://codepad.org/9D26wLEA

Segmentation fault (core dumped)

c:http://codepad.org/JmemaXOr

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

如何让我的$ gcc arrays.c -o arrays -O3 -w $ time ./arrays real 0m0.131s user 0m0.091s sys 0m0.040s 文件变得更好?

2 个答案:

答案 0 :(得分:0)

您可以尝试通过指针直接将第二个字符串添加到内存中第一个字符串的末尾来连接C中的字符串。

 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
 }

这将返回指向新连接字符串末尾的指针,因此您只需传递指针即可继续连接到当前字符串。或者,如果不需要进一步连接,则可以将指针保持为连接字符串的头部。

答案 1 :(得分:0)

为了将int转换为字符串,有人给出了比snprintf快得多的算法: How to convert an int to string in C

这个算法(我将其命名为xitoa)也比PHP脚本快。 (我用int32测试,而不是int64,但它说明了对snprintf的显着改进)

我的基准:

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

这些结果是通过gcc优化-O2(对于snprintf和xitoa)获得的。

这是我测试的算法(从给定链接复制):

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;
}
相关问题