]> sjero.net Git - wget/commitdiff
[svn] Switch to binary search for find_tag.
authorhniksic <devnull@localhost>
Wed, 8 Oct 2003 16:00:10 +0000 (09:00 -0700)
committerhniksic <devnull@localhost>
Wed, 8 Oct 2003 16:00:10 +0000 (09:00 -0700)
src/ChangeLog
src/html-url.c

index 85b0f3da2830689921fce3a760aba9dc1f5cd17b..baa4c581a3513fc1c868c46d754c353227422404 100644 (file)
@@ -1,3 +1,7 @@
+2003-10-08  Hrvoje Niksic  <hniksic@xemacs.org>
+
+       * html-url.c (find_tag): Switch to binary search.
+
 2003-10-08  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * main.c (print_help): Fix typo; stured -> stored.
index 756bf2abb128180e539bade30815027430cf9c9f..abaa2f8e59bcdc18007caa60c4c55fbc75f832e8 100644 (file)
@@ -270,30 +270,36 @@ init_interesting (void)
   }
 }
 
+/* Find tag with name TAG_NAME in KNOWN_TAGS and return its index.  */
+
 static int
 find_tag (const char *tag_name)
 {
-  int i;
+  /* Originally implemented as linear search.  In Wget 1.9 known_tags
+     contains 21 elements, for which binary search requires max. 5
+     comparisons, whereas linear search performs 10 on average.  */
 
-  /* This is linear search; if the number of tags grow, we can switch
-     to binary search.  */
+  int lo = 0, hi = countof (known_tags) - 1;
 
-  for (i = 0; i < countof (known_tags); i++)
+  while (lo <= hi)
     {
-      int cmp = strcasecmp (known_tags[i].name, tag_name);
-      /* known_tags are sorted alphabetically, so we can
-         micro-optimize.  */
-      if (cmp > 0)
-       break;
-      else if (cmp == 0)
-       return i;
+      int mid = (lo + hi) >> 1;
+      int cmp = strcasecmp (tag_name, known_tags[mid].name);
+      if (cmp < 0)
+       hi = mid - 1;
+      else if (cmp > 0)
+       lo = mid + 1;
+      else
+       return mid;
     }
+
   return -1;
 }
 
 /* Find the value of attribute named NAME in the taginfo TAG.  If the
    attribute is not present, return NULL.  If ATTRIND is non-NULL, the
    index of the attribute in TAG will be stored there.  */
+
 static char *
 find_attr (struct taginfo *tag, const char *name, int *attrind)
 {