Skip to content

Commit

Permalink
Use memchr for performance
Browse files Browse the repository at this point in the history
```ruby
s = "b"
str = ("a" * 100 + s)

t = Time.now
str.index(s)
puts Time.now - t
```

before => 0.000788
after  => 0.000508

---

```ruby
s = "b"
str = ("a" * 100 * 1024 * 1024 + s)

t = Time.now
str.index(s)
puts Time.now - t
```

before => 0.225474
after  => 0.008658
  • Loading branch information
ksss committed Dec 31, 2015
1 parent d3c6faf commit 6c1b6ef
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,12 +352,12 @@ mrb_memsearch(const void *x0, mrb_int m, const void *y0, mrb_int n)
return 0;
}
else if (m == 1) {
const unsigned char *ys = y, *ye = ys + n;
for (; y < ye; ++y) {
if (*x == *y)
return y - ys;
}
return -1;
const unsigned char *ys = memchr(y, *x, n);

if (ys)
return ys - y;
else
return -1;
}
return mrb_memsearch_qs((const unsigned char *)x0, m, (const unsigned char *)y0, n);
}
Expand Down

0 comments on commit 6c1b6ef

Please sign in to comment.