简体   繁体   English

C中的高效字符串连接

[英]Efficient string concatenation in C

Here's my problem: I have an array, which contains a command a[1], followed by several command args a[2], a[3], ... 这是我的问题:我有一个数组,其中包含命令a [1],后跟几个命令args a [2],a [3],...

What I need to do is the following 我需要做的是以下几点

  • Create a string, consisting of the cmd and a combination of args Eg: 创建一个字符串,该字符串由cmd和args的组合组成,例如:

cmd arg1 arg2 arg3 cmd arg1 arg2 arg3

  • Execute that command-string 执行该命令字符串

Here's how I woud do it (pseudo-code): 这是我要怎么做(伪代码):

  1. Precompute the length of each arg and store it in an array 预计算每个arg的长度并将其存储在数组中
  2. Get a combination (using GNU Scientific Library) 获取组合(使用GNU科学库)
  3. Compute the size in bytes needed to allocate the string (length of cmd + 1 + lengthof arg1 + 1 + argn-1 + 1) (the +1 generally for the for the blank and at the end for the \\0) 计算分配字符串所需的大小(以字节为单位)(cmd的长度+ 1 + arg1的长度+ 1 + argn-1的长度)(对于空格,+1表示,\\ 0末尾表示+1)
  4. Build the string by using strcat 通过使用strcat构建字符串
  5. Execute the command-string 执行命令字符串

Well, it works but I wonder if using strcat that deliberately is actually efficient/the right way to do it. 好吧,它起作用了,但是我想知道是否故意使用strcat实际上是有效的/正确的方法。

Any suggestions? 有什么建议么?

No, using strcat() is not efficient since it has to step through the string to find the end each time you call it. 不,使用strcat()效率不高,因为每次调用它都必须逐步遍历字符串以查找结尾。

Much better to either do it all at once using snprintf() if you have it (and can squeeze your arguments in there), or do it yourself using direct pointer manipulation. 如果有的话,最好一次使用snprintf()所有操作(并且可以在其中挤压参数),或者直接使用指针操作自己做。

Of course, for this to matter in practice you need to be running this command very often indeed. 当然,要使这在实践中起作用,您确实确实需要经常运行此命令。

如果您已经存储了每个组件字符串的长度,则可以切换到使用具有正确指针偏移量的memcpy而不是使用strcat ,因为strcat不必查找字符串的末尾并测试每个源字符是否针对'\\0' ,但是除此之外,您还可以做很多事情,以使创建串联的速度更快。

strcat() , as well as all string manipulation functions from the standard library, is inefficient. strcat()以及标准库中的所有字符串操作函数效率低下。 this is due to way strings are stored in C, namely zero-terminated, thus each function has to find the end of the string by iterating over each character. 这是由于字符串存储在C中的方式,即以零结尾,因此每个函数都必须通过遍历每个字符来找到字符串的结尾。

anyway, you are doing a premature optimization: the multiple strcat() calls here will execute really fast compared to the command execution, so you should not worry about the efficiency of your way of concatenating. 无论如何,您正在做一个过早的优化:与命令执行相比,这里的多个strcat()调用执行起来确实非常快,因此您不必担心连接方式的效率。

before optimizing part of a code, you have to show that it is a bottleneck and that optimizing will really improve execution time. 在优化代码的一部分之前,您必须证明这是一个瓶颈,并且优化确实会缩短执行时间。 in most cases, there is no need to optimize: it is simply not worth the time spent. 在大多数情况下,无需进行优化:根本不值得花费时间。

我会用sprintf做到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM