This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 5th, 2012  |  syntax: D  |  size: 2.56 KB  |  views: 45  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import std.stdio;
  2. import std.datetime;
  3. import std.range;
  4. import std.conv;
  5. import std.string;
  6.  
  7. //import std.regex;
  8. //import core.thread; //Thread.sleep(dur!("seconds")(1));
  9. const SEC_CONV  = 0.0000001;
  10. const MAX_RANGE = 1000000;
  11.  
  12. __gshared string[int] table;
  13. shared static this()
  14. {
  15.     foreach (i; 0 .. MAX_RANGE)
  16.         table[i] = to!string(i);
  17. }
  18.  
  19. long print_start(string s)
  20. {
  21.     write(format(s, MAX_RANGE));
  22.     stdout.flush();
  23.     return Clock.currStdTime();
  24. }
  25.  
  26. void print_end(long start)
  27. {
  28.     writef("%.3fs\n", (Clock.currStdTime() - start) * SEC_CONV);
  29. }
  30.  
  31. void test_dict()
  32. {
  33.     //[2012-04-05] set 2.251s get 1.957s clear 0.000s
  34.     long start;
  35.     string[string] d;
  36.     string s;
  37.     write("---- test_dict -----\n");
  38.  
  39.     start = print_start("test dict set (%d)...\t");
  40.  
  41.     for (int i = 0; i < MAX_RANGE; i++)
  42.     {
  43.         //~ s    = to!string(i);
  44.         s    = hash[i];;
  45.         d[s] = s;
  46.     }
  47.  
  48.     print_end(start);
  49.  
  50.     start = print_start("test dict get (%d)...\t");
  51.  
  52.     for (int i = 0; i < MAX_RANGE; i++)
  53.     {
  54.         s = d[hash[i]];
  55.     }
  56.  
  57.     print_end(start);
  58.  
  59.     start = print_start("test dict clear (%d)...\t");
  60.     d.clear();
  61.     print_end(start);
  62. }
  63.  
  64. void test_list()
  65. {
  66.     //[2012-04-05] set 0.085s get 0.007s each 0.014s
  67.     long start;
  68.     long j;
  69.     long[] l;
  70.     write("---- test_list -----\n");
  71.  
  72.     start = print_start("test list set (%d)...\t");
  73.  
  74.     for (int i = 0; i < MAX_RANGE; i++)
  75.     {
  76.         l ~= i;
  77.     }
  78.  
  79.     print_end(start);
  80.  
  81.     start = print_start("test list get (%d)...\t");
  82.  
  83.     for (int i = 0; i < MAX_RANGE; i++)
  84.     {
  85.         j = l[i];
  86.     }
  87.  
  88.     print_end(start);
  89.  
  90.     start = print_start("test list each (%d)...\t");
  91.  
  92.     for (int i = 0; i < l.length; i++)
  93.     {
  94.         j = l[i];
  95.     }
  96.  
  97.     print_end(start);
  98. }
  99.  
  100. void test_str()
  101. {
  102.     //[2012-04-05] find 1.254s replace 1.870s
  103.     long   start;
  104.     long   j;
  105.     const  s = "1143534=69789+35435^(100&10+5)";
  106.     const  rep_before = "35";
  107.     const  rep_after  = "3535";
  108.     string ss;
  109.     write("---- test_str -----\n");
  110.  
  111.     start = print_start("test str find (%d)...\t");
  112.  
  113.     for (int i = 0; i < MAX_RANGE; i++)
  114.     {
  115.         j = s.indexOf(hash[i]);
  116.     }
  117.  
  118.     print_end(start);
  119.  
  120.     start = print_start("test str replace (%d)...\t");
  121.  
  122.     for (int i = 0; i < MAX_RANGE / 2; i++)
  123.     {
  124.         ss = s.replace(rep_before, rep_after);
  125.         ss = ss.replace(rep_after, rep_before);
  126.     }
  127.  
  128.     print_end(start);
  129. }
  130.  
  131. void main()
  132. {
  133.     test_dict();
  134.     test_list();
  135.     test_str();
  136. }
clone this paste RAW Paste Data