This variant is in my tests about 7-10% faster, and I also think
it is perhaps even clearer code than before.
I assume that more will do the testing, I do not know if we should do tests
on different types of hardware as well, my test was on a new Intel I7.
Rickard Strandqvist (1):
lib: string.c: A speed optimized for strncpy
lib/string.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
--
1.7.10.4
This variant is in my tests about 7-10% faster, and also think
it is perhaps even clearer code than before.
Signed-off-by: Rickard Strandqvist <[email protected]>
---
lib/string.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/lib/string.c b/lib/string.c
index f3c6ff5..6961229 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -123,12 +123,12 @@ char *strncpy(char *dest, const char *src, size_t count)
{
char *tmp = dest;
- while (count) {
- if ((*tmp = *src) != 0)
- src++;
- tmp++;
- count--;
- }
+ while (count && (*tmp++ = *src++))
+ --count;
+
+ while (count--)
+ *tmp++ = '\0';
+
return dest;
}
EXPORT_SYMBOL(strncpy);
--
1.7.10.4
On Sun, 2014-10-05 at 15:29 +0200, Rickard Strandqvist wrote:
> This variant is in my tests about 7-10% faster, and also think
> it is perhaps even clearer code than before.
[]
> diff --git a/lib/string.c b/lib/string.c
[]
> @@ -123,12 +123,12 @@ char *strncpy(char *dest, const char *src, size_t count)
> {
> char *tmp = dest;
>
> - while (count) {
> - if ((*tmp = *src) != 0)
> - src++;
> - tmp++;
> - count--;
> - }
> + while (count && (*tmp++ = *src++))
> + --count;
> +
> + while (count--)
> + *tmp++ = '\0';
Perhaps it could be faster to use memset.
It might depend on the value of count.
{
while (count && (*tmp++ = *src++))
count--;
if (count > 0)
memset(tmp, 0, count);
}
Hi
Yes, it can be faster, even if it is as you say, probably a difference
depending on the size of the count.
And even greater need to test this on a variety of hardware :-/
But I try to do my test with the memset variant to.
Kind regards
Rickard Strandqvist
2014-10-05 17:36 GMT+02:00 Joe Perches <[email protected]>:
> On Sun, 2014-10-05 at 15:29 +0200, Rickard Strandqvist wrote:
>> This variant is in my tests about 7-10% faster, and also think
>> it is perhaps even clearer code than before.
> []
>> diff --git a/lib/string.c b/lib/string.c
> []
>> @@ -123,12 +123,12 @@ char *strncpy(char *dest, const char *src, size_t count)
>> {
>> char *tmp = dest;
>>
>> - while (count) {
>> - if ((*tmp = *src) != 0)
>> - src++;
>> - tmp++;
>> - count--;
>> - }
>> + while (count && (*tmp++ = *src++))
>> + --count;
>> +
>> + while (count--)
>> + *tmp++ = '\0';
>
> Perhaps it could be faster to use memset.
> It might depend on the value of count.
>
> {
> while (count && (*tmp++ = *src++))
> count--;
>
> if (count > 0)
> memset(tmp, 0, count);
> }
>
On Sun, Oct 05, 2014 at 06:01:43PM +0200, Rickard Strandqvist wrote:
> Hi
>
> Yes, it can be faster, even if it is as you say, probably a difference
> depending on the size of the count.
> And even greater need to test this on a variety of hardware :-/
Most architectures (the notable exception is ARM) have an their own
optimized strncpy() function. Probably strzcpy() should just call it.
char *strzcpy(char *dest, const char *src, size_t count)
{
strncpy(dest, src, count);
if (count)
dest[count - 1] = '\0';
return dest;
}
regards,
dan carpenter