小编Xan*_*ndy的帖子

在C中连接字符串,哪种方法更有效?

我遇到了这两种连接字符串的方法:

共同部分:

char* first= "First";
char* second = "Second";
char* both = malloc(strlen(first) + strlen(second) + 2);
Run Code Online (Sandbox Code Playgroud)

方法1:

strcpy(both, first);
strcat(both, " ");       // or space could have been part of one of the strings
strcat(both, second);
Run Code Online (Sandbox Code Playgroud)

方法2:

sprintf(both, "%s %s", first, second);
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,内容both都是"First Second".

我想知道哪一个更有效(我必须执行几个连接操作),或者如果你知道更好的方法来做到这一点.

c string performance concatenation

56
推荐指数
4
解决办法
13万
查看次数

C中的微优化,有哪些?有没有人真正有用?

我理解大部分的微优化,但它们真的有用吗?

Exempli特惠:不这样做++i,而不是i++,或while(1)还是for(;;)真的导致性能改进(在内存指纹或CPU周期)?

所以问题是,在C中可以进行哪些微优化?它们真的有用吗?

c micro-optimization

3
推荐指数
2
解决办法
1198
查看次数