Skip to content

Instantly share code, notes, and snippets.

@objmagic
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save objmagic/e457ca83dd97a3e6005f to your computer and use it in GitHub Desktop.

Select an option

Save objmagic/e457ca83dd97a3e6005f to your computer and use it in GitHub Desktop.
regex contest with Intel(R) Core(TM) i7-5820K CPU @ 3.30GHz
/*
$ /usr/bin/g++ -O2 -std=c++11
$ time ./a.out
0.52s user 0.00s system 99% cpu 0.525 total
*/
#include <regex>
#include <string>
#include <iostream>
using namespace std;
int main() {
string s(1048576, 'a');
s.back() = 'b';
string re(400, ' ');
for (int i = 0; i < 400; i+=4) {
re[i] = 'a';
re[i+1] = 'a';
re[i+2] = '?';
re[i+3] = 'b';
}
regex r(re);
for(int i = 0; i < 10; i++)
regex_search(s, r);
return 0;
}
(* $ corebuild -pkg core,re.posix test.native
$ time ./test.native
0.09s user 0.00s system 95% cpu 0.090 total
*)
open Core.Std
let str =
let len = 1048576 in
let str = String.create len in
String.fill str ~pos:0 ~len:len 'a';
str.[len - 1] <- 'b';
str
let pat =
let str = String.create 400 in
let i = ref 0 in
while !i < 400 do
str.[!i] <- 'a';
str.[!i + 1] <- 'a';
str.[!i + 2] <- '?';
str.[!i + 3] <- 'b';
i := !i + 4
done;
str
let compiled_pat = Re_posix.compile_pat pat
let () =
for i = 0 to 10 do
ignore (Re.execp compiled_pat str)
done
/* /usr/bin/g++ -O2 -std=c++11 b.cc -lre2 && time ./a.out
0.12s user 0.00s system 99% cpu 0.121 total
*/
#include <string>
#include <iostream>
#include <re2/re2.h>
 
using namespace std;
 
int main() {
  string s(1048576, 'a');
  s.back() = 'b';
  string re(400, ' ');
  for (int i = 0; i < 400; i+=4) {
    re[i] = 'a';
    re[i+1] = 'a';
    re[i+2] = '?';
    re[i+3] = 'b';
  }
  re2::RE2 r(re);
  for(int i = 0; i < 10; i++)
    RE2::PartialMatch(s, r);
  return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment