Advertisement
DigitalMag

Regex python/node bench

Feb 10th, 2020
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. import re
  2.  
  3. def main(s):
  4.     r = re.compile('\d+')
  5.     rez = ''
  6.     for i in range(100000):
  7.         #s.replace('4','')
  8.         rez = r.sub('',s)
  9.  
  10.     return rez
  11.  
  12. if __name__ == '__main__':
  13.  
  14.     import time
  15.  
  16.     for i in range(5):
  17.         start = time.time()
  18.  
  19.         print main('str4')
  20.  
  21.         end = time.time()
  22.         print(end - start)
  23.  
  24.  
  25.  
  26. # vs:
  27.  
  28. const performance = require('perf_hooks').performance;
  29.  
  30. function Test(str){
  31.  
  32.     const regex = new RegExp("\d+");// /\d+/gi;
  33.  
  34.     var rez = 0;
  35.     for(var i=0;i<100000;i++){
  36.         rez = str.replace(regex, '');
  37.     }  
  38.     return rez;
  39.  
  40.    
  41. }
  42.  
  43. function apply(){
  44.     var time = performance.now();
  45.  
  46.     console.log(Test('str4'));
  47.  
  48.  
  49.     time = performance.now()- time;
  50.     console.log('Время выполнения 1 = ', time);
  51. }
  52.  
  53.  
  54.  
  55. for (var a=0;a<5;a++){
  56.     apply();
  57. }
  58.  
  59.  
  60.  
  61.  
  62. // rez: js - 52ms/25ms
  63. # rez p - 350ms/350ms
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement