C 中的高效字符串连接

发布于 2024-08-21 16:14:52 字数 510 浏览 3 评论 0原文

这是我的问题:我有一个数组,其中包含命令 a[1],后跟几个命令参数 a[2]、a[3]、...

我需要做的是

  • 创建一个字符串,其中包含cmd 和 args 的组合 例如:

cmd arg1 arg2 arg3

  • 执行该命令字符串

以下是我的做法(伪代码):

  1. 预先计算每个 arg 的长度并将其存储在数组中
  2. 获取组合(使用 GNU 科学库)
  3. 计算分配所需的大小(以字节为单位)字符串(cmd 的长度 + 1 + lengthof arg1 + 1 + argn-1 + 1) (+1 通常用于空格和 at \0 的结尾)
  4. 使用 strcat 构建字符串
  5. 执行命令字符串

好吧,它可以工作,但我想知道故意使用 strcat 是否实际上是有效的/正确的方法。

有什么建议吗?

Here's my problem: I have an array, which contains a command a[1], followed by several command 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
    E.g.:

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
  2. Get a combination (using GNU Scientific Library)
  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)
  4. Build the string by using 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.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾铮苏瑾 2024-08-28 16:14:52

不,使用 strcat() 效率不高,因为每次调用它时它都必须逐步遍历字符串才能找到结尾。

如果您有的话,最好使用 snprintf() 一次完成所有操作(并且可以将参数压缩到其中),或者使用直接指针操作自己完成。

当然,为了在实践中发挥作用,您确实需要经常运行此命令。

No, using strcat() is not efficient since it has to step through the string to find the end each time you call it.

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.

Of course, for this to matter in practice you need to be running this command very often indeed.

黎歌 2024-08-28 16:14:52

如果您已经存储了每个组件字符串的长度,则可以切换为使用具有正确指针偏移量的memcpy,而不是使用strcat,后者不必找到该结尾字符串并针对 '\0' 测试每个源字符,但除此之外,您没有什么可以做的来使连接的创建速度显着加快。

If you've stored the lengths of each component string that you can switch to using memcpy with the correct pointer offsets instead of using strcat which won't have to find that end of the string adn the test each source char against '\0', but other than that there isn't a whole lot more that you can do to make the creation of a concatenated significantly faster.

哽咽笑 2024-08-28 16:14:52

strcat() 以及标准库中的所有字符串操作函数效率低下。这是由于字符串在 C 中的存储方式所致,即以零结尾,因此每个函数都必须通过迭代每个字符来找到字符串的结尾。

无论如何,您正在进行过早的优化:与命令执行相比,此处的多个 strcat() 调用执行速度非常快,因此您不必担心连接方式的效率。

在优化部分代码之前,您必须表明它是一个瓶颈,并且优化确实会缩短执行时间。在大多数情况下,不需要优化:根本不值得花费时间。

strcat(), as well as all string manipulation functions from the standard library, is inefficient. 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.

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.

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.

寒江雪… 2024-08-28 16:14:52

我会用 sprintf 来完成。

I would do it with sprintf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文