• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 單元測試初探之Java正則表達式引發的思考(2)

    發表于:2012-11-21來源:淘寶網綜合業務平臺團隊博客作者:xuanyin點擊數: 標簽:java正則表達式
    import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassWriter; public class MethodCallCountTransformer implements ClassFileTransformer { @Override public byte[] transform(ClassLoader load

      import org.objectweb.asm.ClassReader;

      import org.objectweb.asm.ClassWriter;

      public class MethodCallCountTransformer implements ClassFileTransformer {

      @Override

      public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined,

      ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {

      try {

      if (!“java/util/regex/Pattern$CharProperty”.equals(className)

      && !“java/util/regex/Pattern$BmpCharProperty”.equals(className)) {

      return classfileBuffer;

      }

      ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);

      ClassAdapter adapter = new MethodCallClassAdapter(writer, className);

      ClassReader reader = new ClassReader(classfileBuffer);

      reader.accept(adapter, 0);

      // 生成新類字節碼

      return writer.toByteArray();

      } catch (Exception e) {

      e.printStackTrace();

      // 返回舊類字節碼

      return classfileBuffer;

      }

      }

      }

      package com.alibaba.taobao.tinyprofiler;

      import java.util.concurrent.atomic.AtomicInteger;

      public class Counter {

      private static AtomicInteger methodCallCount = new AtomicInteger(0);

      public static void printAndIncCount(String className, String methodName) {

      System.out.println(className + “.” + methodName + “ called, total times ” + methodCallCount.incrementAndGet());

      }

      }

      OK,現在我們輸入:

      String regex = “.*.*.*D”;

      String target = “22asdvasdx”;

      Pattern.compile(regex).matcher(target).matches();

      System.out.println(“Xuanyin’s estimated count: ” + findTotalWays(4, target.length()) + “; depth: ” + recursionDepth);

      輸出結果(注:這里的 depth 并不是遞歸深度,而是遞歸次數,當時搞錯了):

      腫么樣,分毫不差~OK,那么我們現在回到最開始的問題,輸入 .*.*.*.*.*.*D 去匹配 com.taobao.binary.bogda.query.service.RulesInfoQueryService:1.0.0.daily

      結果顯示需要 5 億 匹配, 還要進出棧近 2.5 億次哦

      這里我的機器是i7-2600K 超4.5G,結果顯示需要5秒,這還是不是最差的情況哦~

    而每次用戶查詢要匹配近600個這樣的字符串,~你說匹配得完嘛

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>