Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

match demo

By: a guest on Jan 1st, 2013  |  syntax: None  |  size: 0.77 KB  |  views: 31  |  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. # same result
  2.  
  3. bash-4.2$ [[ 'test' == ?e* ]]; echo $?
  4. 0
  5.  
  6. bash-4.2$ [[ 'test' == ?x* ]]; echo $?
  7. 1
  8.  
  9. bash-4.2$ [[ 'test' =~ .e.* ]]; echo $?
  10. 0
  11.  
  12. bash-4.2$ [[ 'test' =~ .x.* ]]; echo $?
  13. 1
  14.  
  15. # but at different speed
  16.  
  17. bash-4.2$ time for x in {1..1000000}; do [[ 'test' == ?e* ]]; done
  18.  
  19. real    0m4.943s
  20. user    0m4.899s
  21. sys     0m0.011s
  22.  
  23. bash-4.2$ time for x in {1..1000000}; do [[ 'test' =~ .e.* ]]; done
  24.  
  25. real    0m17.056s
  26. user    0m16.962s
  27. sys     0m0.010s
  28.  
  29. # measure the loop's own speed to subtract it
  30.  
  31. bash-4.2$ time for x in {1..1000000}; do :; done
  32.  
  33. real    0m4.357s
  34. user    0m4.329s
  35. sys     0m0.009s
  36.  
  37. # to find out the times actually spent for matching
  38. # - [[ 'test' == ?e* ]]   4.943 - 4.357 =  0.586
  39. # - [[ 'test' =~ .e.* ]] 17.056 - 4.357 = 12.699