| View previous topic :: View next topic |
| Author |
Message |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Thu Aug 06, 2009 1:17 pm Post subject: [JS] Which is faster? |
|
|
Which code would execute faster?
Say you have | Code: | | var str = ' hello '; | and you wantwould | Code: | | str = str.replace(/[\s]+$/g, '').replace(/^[\s]+/g,'') | or | Code: | for(i=0;i<str.length;i++){
if(str[i] != ' '){
break;
}
}
str = str.substring(i);
for(i=str.length-1;i>=0;i--){
if(str[i] != ' '){
break;
}
}
str = str.substring(0,i+1); | be faster? (I'm not including time downloading the code)
Thanks
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Thu Aug 06, 2009 1:22 pm Post subject: |
|
|
| the second one is faster
|
|
| Back to top |
|
 |
DanielG Expert Cheater
Reputation: 1
Joined: 13 May 2009 Posts: 130 Location: The Netherlands
|
Posted: Thu Aug 06, 2009 1:29 pm Post subject: |
|
|
In general RegEx is slower.
But you should conduct a benchmark test and time it.
|
|
| Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Thu Aug 06, 2009 1:53 pm Post subject: |
|
|
Okay I've tried to benchmark it with | Code: | var i = 0,
j = 0,
sum = [],
d = null,
n = 100000;
var str = '';
d = (new Date).getTime();
for(j=0;j<n;j++){
str = ' hello ';
str = str.replace(/[\s]+$/g, '').replace(/^[\s]+/g,'');
}
sum[0] = ((new Date).getTime() - d);
d = (new Date).getTime();
for(j=0;j<n;j++){
str = ' hello ';
for(i=0;i<str.length;i++){
if(str[i] != ' '){
break;
}
}
str = str.substring(i);
for(i=str.length-1;i>=0;i--){
if(str[i] != ' '){
break;
}
}
str = str.substring(0,i+1);
}
sum[1] = ((new Date).getTime() - d);
[sum[0]/n, sum[1]/n]; | and it is giving me | Code: | n = 10 -> [0, 0]
n = 100 -> [0.01, 0.02]
n = 1000 -> [0.01, 0.016]
n = 10000 -> [0.0088, 0.0162]
n = 100000 -> [0.00877, 0.01648] | which suggests the regex is faster in this case.. but I'm not sure if it is recreating the regex or just keeping them in memory.. you know which?
(Tested using Firebug, Firefox)
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 475
Joined: 09 May 2003 Posts: 25980 Location: The netherlands
|
Posted: Thu Aug 06, 2009 3:50 pm Post subject: |
|
|
Instead of calling str.substring you can limit it to 1 time
find the first non space character (A)
count the number of characters there are in the string (so first non space from the end -A)
and use str.substring string from position A for the number of chars the string is
_________________
Tools give you results. Knowledge gives you control.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Thu Aug 06, 2009 4:10 pm Post subject: |
|
|
Read edit below line
Thanks for the suggestion but changing from using str.substring twice to using it once that only got it down to 0.0154, which is still slower
My benchmark code | Code: | var i = 0,
j = 0,
k = 0,
sum = [],
d = null,
n = 10000;
var str = '';
d = (new Date).getTime();
for(k=0;k<n;k++){
str = ' hello ';
str = str.replace(/[\s]+$/g, '').replace(/^[\s]+/g,'');
}
sum[0] = ((new Date).getTime() - d);
d = (new Date).getTime();
for(k=0;k<n;k++){
str = ' hello ';
for(i=0;i<str.length;i++){
if(str[i] != ' '){
break;
}
}
for(j=str.length-1;j>=0;j--){
if(str[j] != ' '){
break;
}
}
str = str.substring(i,j+1);
}
sum[1] = ((new Date).getTime() - d);
[sum[0]/n, sum[1]/n]; | result
//--Edit----
Sorry maybe I misunderstood first time around, I've got it down to 0.0056 now by only using one loop.
I didn't realise doing more than twice as many 'if's inside one loop was faster than doing two loops.
Code now | Code: | var i = 0,
j = 0,
k = 0,
startend = [0,0],
sum = [],
d = null,
n = 10000;
var str = '';
d = (new Date).getTime();
for(k=0;k<n;k++){
str = ' hello ';
str = str.replace(/[\s]+$/g, '').replace(/^[\s]+/g,'');
}
sum[0] = ((new Date).getTime() - d);
d = (new Date).getTime();
for(k=0;k<n;k++){
str = ' hello ';
for(i=0;i<str.length;i++){
if(!startend[0]){
if(str[i] != ' '){
startend[0] = i;
}
}
if(!startend[1]){
if(str[str.length-i-1] != ' '){
startend[1] = str.length-i;
}
}
if(startend[0] && startend[1]){
break;
}
}
str = str.substring(startend[0],startend[1]);
}
sum[1] = ((new Date).getTime() - d);
[sum[0]/n, sum[1]/n]; |
I'll clean it up a bit before putting it into practice but thanks again!
edit2: actually; that doesn't work because I forgot strings start at 0.
edit3: easy workaround: +1 then -1 in the substring
Last edited by shhac on Thu Aug 06, 2009 5:46 pm; edited 3 times in total |
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 475
Joined: 09 May 2003 Posts: 25980 Location: The netherlands
|
Posted: Thu Aug 06, 2009 4:16 pm Post subject: |
|
|
perhaps str.replace just has faster string reading routines than you can make yourself. (e.g optimized java runtime, especially if it has access to sse4.2 instructions that have new instructions for string scanning)
Also, what I always ask myself when optimizing, is the speed really so important in this routine. Are you going to call it 10000 times a second ?
_________________
Tools give you results. Knowledge gives you control.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Thu Aug 06, 2009 4:23 pm Post subject: |
|
|
| No I'm not going to call it 10,000 times a second but it is something I want to require low processor usage so it can be used in a loop with something else that might enjoy eating cycles.
|
|
| Back to top |
|
 |
DanielG Expert Cheater
Reputation: 1
Joined: 13 May 2009 Posts: 130 Location: The Netherlands
|
Posted: Thu Aug 06, 2009 6:02 pm Post subject: |
|
|
I've ran the javascript "Web Browser Javascript Benchmark".
And there RegEx operations are slower than String operations
(RegEx object: 31ms and String object 18ms.)
|
|
| Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Fri Aug 07, 2009 3:20 pm Post subject: |
|
|
| This was a case-specific thing for a trim function, not a general "anything" question. Thanks for your input though.
|
|
| Back to top |
|
 |
|