【问题标题】:What is Best way concatenate strings and number - Performance Using C?连接字符串和数字的最佳方法是什么 - 使用 C 的性能?
【发布时间】:2017-06-16 18:57:32
【问题描述】:

请下去阅读new/last update部分。

我非常尝试编写性能良好的代码。

而且php interpreter script 比我的c app 更快。

我正在一个大循环中测试这个。我确定我的连接代码的速度很差。 然后肯定可以使它像 php 脚本一样好。


比较源(c):

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

str_int(...)功能:

#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;
}

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

时间:0m0.160s


但是 PHP 7.1.4 : 0.081s

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

请帮我让这个 c 文件更快...

我想让我的 c 代码更好。

php 在连接 string,int 方面具有更高的性能。 但是我的c代码不像他们。

怎样才能做得更好?

非常感谢你。 :喜欢:

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

答案 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;
}

错误:Segmentation fault (core dumped)


性能不佳的新/最后更新(C 与 PHP)

php(最新版本):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

如何让我的C 文件更好?

【问题讨论】:

  • “请帮助我,不要关闭问题。” ,这是题外话,您可能会在code review stack exchange 上获得更多运气。
  • 我只是问了一个问题,那么如何才能做得更好?!
  • 我怀疑你使用的是Shlemiel's algorithm,也许是strcat()
  • 意思是使用strcat() ?那么如何使用 strcat() 将 int 附加到 char* 以提高性能?
  • sizeof(s1) 并不像您认为的那样 - 它返回指针的大小,而不是您需要将字符串复制到的缓冲区的大小。我什至不完全确定这个表达式的 intent 是什么:sizeof(s1)+sizeof(s2)+2*sizeof(DATA_VALUE_String *)。在您过分担心性能之前,请确保代码正确。

标签: c string performance


【解决方案1】:

您可以尝试在 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
 }

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

【讨论】:

  • 坦克你。如何将 int 附加到具有更高性能的字符串?
  • 我认为这个函数比 strcat 更有效,但是,你需要测试从 int64 到 char* 的转换是否会导致它比你尝试的方法更糟糕。见:stackoverflow.com/questions/9695720/…
  • #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;inttypes.h&gt; 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(&amp;nums,num); printf("%s",nums); return 0; } ==========>错误:分段错误(核心转储)
  • 问:你确定你的strConcat()函数支持utf-8 unicode吗?
【解决方案2】:

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

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

我的基准:

  • 使用 snprintf:1.54 秒
  • 与 xitoa:0.99 秒
  • 使用 PHP:1.23 秒

这些结果是通过 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;
}

【讨论】:

  • 问:你确定xitoa()支持utf-8吗?
  • 问:-O3 不是更好的-O2 吗?
  • 可能会说一个示例代码然后如何使用这个函数?
  • char str[5];printf("%s\n", xitoa(15,str,2) );,输出为:1111
  • 为什么输出是1111
猜你喜欢
  • 2014-03-19
  • 2010-09-24
  • 2016-12-11
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多