我遇到了这两种连接字符串的方法:
共同部分:
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".
我想知道哪一个更有效(我必须执行几个连接操作),或者如果你知道更好的方法来做到这一点.
我理解大部分的微优化,但它们真的有用吗?
Exempli特惠:不这样做++i,而不是i++,或while(1)还是for(;;)真的导致性能改进(在内存指纹或CPU周期)?
所以问题是,在C中可以进行哪些微优化?它们真的有用吗?