正規表現ベンチとやら

いまさらhttp://www.ketan.jp/diary/の2008-05-28の話。ライブラリ編。
結果がつまらないので放置しておいた結果がこれだよ!
どれもWindows XP Home SP3で動かしたから他の環境では知らないよ。
みんな初めて使ったライブラリなので、使い方間違ってたらごめんなさい。

C# + .NET

using System.Text.RegularExpressions;
using System.Text;

public class RegexTest
{
    private static int len = 1024 * 1024;

    public static void Main()
    {
        StringBuilder builder = new StringBuilder(len+1);
        for (int i = 0; i < 1024 * 1024; ++i)
            builder.Append('b');
        builder.Append('x');

        string query = builder.ToString();

        Regex regex = new Regex("^b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b$");
        if (regex.IsMatch(query))
            System.Console.Out.WriteLine("yes");
        else
            System.Console.Out.WriteLine("no");
    }
}

結果:応答なし

JavaScript + Webブラウザ(IE7, Firefox3, Opera9)

javascript:query="";for(i=0;i<1024*1024;++i)query+="b";query+="x";alert(query.match(/^b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b$/)?"yes":"no")

結果:応答なし

boost::regex

#include <iostream>
#include <string>
#include <boost/regex.hpp>

using namespace std;

int main() {
  boost::regex regex("^b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b$");

  int len = 1024 * 1024;
  string query(len+1, 'b');
  query[len] = 'x';

  if (boost::regex_match(query, regex))
    cout << "yes" << endl;
  else
    cout << "no" << endl;
}

結果:応答なし

鬼車 v5.9.1

#include <stdio.h>
#include <string.h>
#include <oniguruma.h>

#define LEN (1024 * 1024)
#define PATTERN "^b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b$"

static char buf[LEN + 2];

int main(void)
{
  int i, r;
  unsigned char *at;
  regex_t* regex;
  OnigErrorInfo einfo;
  OnigRegion *region;

  for (i = 0; i < LEN; ++i)
    buf[i] = 'b';
  buf[LEN] = 'x';
  buf[LEN+1] = '\0';

  static UChar* pattern = (UChar* )PATTERN;
  static UChar* query = (UChar* )buf;

  r = onig_new(&regex, pattern, pattern + strlen((char* )pattern),
               ONIG_OPTION_DEFAULT, ONIG_ENCODING_ASCII, ONIG_SYNTAX_DEFAULT, &einfo);
  if (r != ONIG_NORMAL) {
    char s[ONIG_MAX_ERROR_MESSAGE_LEN];
    onig_error_code_to_str(s, r, &einfo);
    fprintf(stderr, "ERROR: %s\n", s);
    return -1;
  }

  region = onig_region_new();
  at = query;

  r = onig_match(regex, query, query + strlen((char*)query), at, region, ONIG_OPTION_NONE);

  onig_region_free(region, 1);
  onig_free(regex);
  onig_end();
  
  if (r >= 0)
    puts("yes");
  else
    puts("no");

  return 0;
}

結果:応答なし

Jakarta-regexp

import org.apache.regexp.RE;

public class Jakarta {
  public static void main(String[] args) {
    StringBuilder buf = new StringBuilder(1024*1024+1);
    for (int i = 0; i < 1024 * 1024; ++i)
      buf.append("b");
    buf.append("x");
    
    String query = new String(buf);
    String pattern = "^b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b.*b$";
    RE regex = new RE(pattern);
    if (regex.match(query))
      System.out.println("yes");
    else
      System.out.println("no");
  }
}

結果:スタックオーバーフロー