/ictclas4j/src/org/ictclas4j/test/UtilityTest.java

http://ictclas4j.googlecode.com/ · Java · 105 lines · 66 code · 19 blank · 20 comment · 11 complexity · 970507d48ba85e0a2a6acb4f08fa04dd MD5 · raw file

  1. package org.ictclas4j.test;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import org.apache.log4j.Logger;
  5. import org.apache.log4j.PropertyConfigurator;
  6. import org.ictclas4j.run.Config;
  7. import org.ictclas4j.util.Utility;
  8. import com.gftech.util.GFString;
  9. //test1
  10. //test2
  11. public class UtilityTest {
  12. static Logger logger = Logger.getLogger(UtilityTest.class);
  13. public static void main(String[] args) {
  14. PropertyConfigurator.configure(Config.LOG4J_CONF);
  15. test3();
  16. }
  17. private static void test1() {
  18. System.out.println(Utility.GBK_ID("?"));
  19. byte[] bs = new byte[2];
  20. // for(int i=0xa1;i<0xb0;i++)
  21. // for(int j=0xa1;j<0xFF;j++){
  22. // bs[0]=(byte)i;
  23. // bs[1]=(byte)j;
  24. // System.out.println(i+","+j+":"+new String(bs));
  25. // }
  26. //
  27. ArrayList<String> list = new ArrayList<String>();
  28. for (int i = 21998; i < 22034; i++) {
  29. String w = Utility.getGBKWord(i);
  30. String str = i + "," + GFString.int2hex(i, 4) + ":" + w;
  31. list.add(str);
  32. System.out.println(str);
  33. if (w != null && Utility.GBK_ID(w) != i) {
  34. System.err.println("\nerr:" + w + "," + Utility.GBK_ID(w));
  35. // break;
  36. }
  37. }
  38. //
  39. // try {
  40. // GFFile.writeTxtFile("test\\gbk_test.txt",list,false);
  41. // } catch (IOException e) {
  42. // e.printStackTrace();
  43. // }
  44. //
  45. }
  46. // test GBK_ID() profile
  47. public static void test2() {
  48. String s = "??????????????c?????????D?????????6?1??3??????????9????????????????????????????????????????????????????????????? ???????????????????????? ???? ?????????a?????????????????????????????";
  49. int count = 0;
  50. HashMap<String, Integer> idMap = new HashMap<String, Integer>();
  51. for (int i = 0; i < Utility.GBK_NUM_EXT; i++) {
  52. idMap.put(Utility.getGBKWord(i), i);
  53. }
  54. String[] ss = new String[s.length()];
  55. for (int j = 0; j < s.length(); j++) {
  56. ss[j] = s.substring(j, j + 1);
  57. }
  58. long start = System.currentTimeMillis();
  59. for (int i = 0; i < 100000; i++) {
  60. for (String s0 : ss) {
  61. count++;
  62. // idMap.get(s0);
  63. Utility.GBK_ID(s0);
  64. }
  65. }
  66. long end = System.currentTimeMillis();
  67. long spend = end - start;
  68. System.out.println("total time:" + spend + ",avg time:" + ((double) spend / count));
  69. }
  70. // test substring's time of String and StringBuffer
  71. public static void test3() {
  72. int count = 0;
  73. String s = "??????????????c?????????D?????????6?1??3??????????9????????????????????????????????????????????????????????????? ???????????????????????? ???? ?????????a?????????????????????????????";
  74. StringBuffer sb=new StringBuffer(s);
  75. long start = System.currentTimeMillis();
  76. for (int i = 0; i < 1000000; i++) {
  77. for (int j = 0; j < s.length(); j++) {
  78. sb.substring(j, j + 1);
  79. count++;
  80. }
  81. }
  82. long end = System.currentTimeMillis();
  83. long spend = end - start;
  84. System.out.println("total time:" + spend + ",avg time:" + ((double) spend / count));
  85. }
  86. }