From 20087bf22698612a526353f022bc232e2b0dcdcc Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 1 Sep 2018 19:00:36 +0100 Subject: lib: Use a bsearch to find the module name Even with a small number of known drivers (6), a bsearch will take at most 3 steps, whereas the linear search will take 3 steps on average. In the future with more known drivers, the logN bsearch will be even more advantageous. Signed-off-by: Chris Wilson Cc: Katarzyna Dec Reviewed-by: Katarzyna Dec --- lib/drmtest.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'lib/drmtest.c') diff --git a/lib/drmtest.c b/lib/drmtest.c index 93228f90..bfb38f1e 100644 --- a/lib/drmtest.c +++ b/lib/drmtest.c @@ -222,9 +222,15 @@ static int open_device(const char *name, unsigned int chipset) if (__get_drm_device_name(fd, dev_name, sizeof(dev_name) - 1) == -1) goto err; - for (const struct module *m = modules; m->module; m++) { - if (strcmp(m->module, dev_name) == 0) { - chip = m->bit; + for (int start = 0, end = ARRAY_SIZE(modules) - 1; start < end; ){ + int mid = start + (end - start) / 2; + int ret = strcmp(modules[mid].module, dev_name); + if (ret < 0) { + end = mid; + } else if (ret > 0) { + start = mid + 1; + } else { + chip = modules[mid].bit; break; } } -- cgit v1.2.3