SHARE
TWEET

match demo

a guest Jan 1st, 2013 55 Never
  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
RAW Paste Data
Top