format: Optimize fmt_from_io(). 20100824040503/pspp
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 16 Aug 2010 05:03:19 +0000 (22:03 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Tue, 24 Aug 2010 03:29:42 +0000 (20:29 -0700)
A "switch" statement should be much faster than a linear search.

src/data/format.c

index aa8723482840dfb05a53057987a5d564d896c59d..3907016cf4dac66ba021694b175e96ef67b2761a 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
+   Copyright (C) 1997-9, 2000, 2006, 2010 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -772,15 +772,16 @@ fmt_to_io (enum fmt_type type)
 bool
 fmt_from_io (int io, enum fmt_type *fmt_type)
 {
-  enum fmt_type type;
-
-  for (type = 0; type < FMT_NUMBER_OF_FORMATS; type++)
-    if (get_fmt_desc (type)->io == io)
-      {
-        *fmt_type = type;
-        return true;
-      }
-  return false;
+  switch (io)
+    {
+#define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY)     \
+    case IO:                                          \
+      *fmt_type = FMT_##NAME;                           \
+      return true;
+#include "format.def"
+    default:
+      return false;
+    }
 }
 
 /* Returns true if TYPE may be used as an input format,