Advertisement
rockdrilla

(da)sh substring match/replace speed tests

Apr 18th, 2018
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.59 KB | None | 0 0
  1. wtf_replace_old() { echo "$@" | sed -re 's/%F/4/g'; }
  2.  
  3. substr_match() {
  4.     local a b
  5.     a=$1; shift; b=${#@}; a=${@#*$a}; a=${#a}
  6.     [ $a -ne $b ]
  7. }
  8.  
  9. wtf_match() { substr_match '%F' "$@"; }
  10.  
  11. # dirty
  12. wtf_replace_new_v1() {
  13.     local x="$@"
  14.     while is_af_loose "$x"; do x=${x%%\%F*}4${x#*%F}; done
  15.     echo "$x"
  16. }
  17.  
  18. # kind of accurate
  19. wtf_replace_new_v2() {
  20.     local x
  21.     for x; do
  22.         while is_af_loose "$x"; do x=${x%%\%F*}4${x#*%F}; done
  23.         if substr_match ' ' "$x"; then echo "'$x'"; else echo "$x"; fi
  24.     done
  25. }
  26.  
  27.  
  28.  
  29. date --rfc-3339=ns
  30. for i in $(seq 1 10000); do
  31.     wtf_replace_old "abc1337def%Fghi${i}klm%Fzyx"
  32. done > /dev/null
  33. date --rfc-3339=ns
  34.  
  35. # 2018-04-19 01:36:38.196838148
  36. # 2018-04-19 01:36:47.792090732
  37.  
  38.  
  39.  
  40. date --rfc-3339=ns
  41. for i in $(seq 1 10000); do
  42.     wtf_replace_new_v1 "abc1337def%Fghi${i}klm%Fzyx"
  43. done > /dev/null
  44. date --rfc-3339=ns
  45.  
  46. # 2018-04-19 01:36:47.792755513
  47. # 2018-04-19 01:36:48.047608340
  48.  
  49.  
  50.  
  51. date --rfc-3339=ns
  52. for i in $(seq 1 10000); do
  53.     wtf_replace_new_v2 "abc1337def%Fghi${i}klm%Fzyx"
  54. done > /dev/null
  55. date --rfc-3339=ns
  56.  
  57. # 2018-04-19 01:36:48.048289043
  58. # 2018-04-19 01:36:48.362512638
  59.  
  60.  
  61.  
  62. date --rfc-3339=ns
  63. for i in $(seq 1 50000); do
  64.     wtf_replace_new_v1 "abc1337def%Fghi${i}klm%Fzyx"
  65. done > /dev/null
  66. date --rfc-3339=ns
  67.  
  68. # 2018-04-19 01:36:48.363150082
  69. # 2018-04-19 01:36:49.632880751
  70.  
  71.  
  72.  
  73. date --rfc-3339=ns
  74. for i in $(seq 1 50000); do
  75.     wtf_replace_new_v2 "abc1337def%Fghi${i}klm%Fzyx"
  76. done > /dev/null
  77. date --rfc-3339=ns
  78.  
  79. # 2018-04-19 01:36:49.633568684
  80. # 2018-04-19 01:36:51.242995435
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement