coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[coreutils] uniq: minor code/perf improvement


From: Jim Meyering
Subject: [coreutils] uniq: minor code/perf improvement
Date: Mon, 17 Jan 2011 12:45:11 +0100

I noticed another wasteful loop in that same function.
The loop can be replaced with a simple "MIN(...)" expression, and that gives
a 10% performance improvement for very large --skip-chars offsets:

    $ (n=1000000000; printf "a%0${n}d0\n" 0;printf "b%0${n}d1\n" 0) \
      | env time --f=%e ./uniq -s$n > /dev/null
    6.03

    $ (n=1000000000; printf "a%0${n}d0\n" 0;printf "b%0${n}d1\n" 0) \
      | env time --f=%e uniq -s$n > /dev/null
    6.80


>From 9aa02d21f75c3c0dd14b4e406ebd0dff0c75504e Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Mon, 17 Jan 2011 12:36:58 +0100
Subject: [PATCH] uniq: replace a wasteful loop with simple calculation

* src/uniq.c (find_field): Remove the byte-skipping loop altogether.
Instead, perform the simple calculation.  This results in a 10%
performance improvement for large byte offsets.
---
 src/uniq.c |    3 +--
 1 files changed, 1 insertions(+), 2 deletions(-)

diff --git a/src/uniq.c b/src/uniq.c
index 9c7e37c..b35938a 100644
--- a/src/uniq.c
+++ b/src/uniq.c
@@ -222,8 +222,7 @@ find_field (struct linebuffer const *line)
         i++;
     }

-  for (count = 0; count < skip_chars && i < size; count++)
-    i++;
+  i += MIN (skip_chars, size - i);

   return line->buffer + i;
 }
--
1.7.3.5



reply via email to

[Prev in Thread] Current Thread [Next in Thread]