Re:Strcpycat
by abecedarius on Friday January 20, 2012 @11:18PM
(#3492584)
It's the second strlen plus the memcpy right after it that I would consider replacing (with a loop that fuses both). (Though since you say it doesn't matter, never mind...)
If you're talking about inputs that aren't arbitrarily huge, you should try measuring performance using alloca instead of malloc.
Yes, this. Alloca should only be a couple of instructions on most modern systems, and there is no needed call to free, which is likely the slower of the two calls..
short answer: I did. It got slower!
They're not doing what you think they are. Each string only gets measured twice.
To factor them out I will need to start allocating extra arrays just to keep their values: the extra malloc and free is slower than measuring the strings twice. I also tried a Reallocing version that didn't need them but it was twice as slow.
I use this code for composing text in places where speed isn't my first concern; especially error messages. But its plenty fast enough for 90% of what anyone would need. You'd rarely be concating more than 6 things together.
note: it's really hard to talk about pointers on HN as you can't type an asterisk without a code block, but it's:
not