Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[JS] Which is faster?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
shhac
Expert Cheater
Reputation: 0

Joined: 30 Oct 2007
Posts: 108

PostPosted: Thu Aug 06, 2009 1:17 pm    Post subject: [JS] Which is faster? Reply with quote

Which code would execute faster?
Say you have
Code:
var str = '   hello   ';
and you want
Code:
str == 'hello'
would
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
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Aug 06, 2009 1:22 pm    Post subject: Reply with quote

the second one is faster
Back to top
View user's profile Send private message
DanielG
Expert Cheater
Reputation: 1

Joined: 13 May 2009
Posts: 130
Location: The Netherlands

PostPosted: Thu Aug 06, 2009 1:29 pm    Post subject: Reply with quote

In general RegEx is slower.
But you should conduct a benchmark test and time it.
Back to top
View user's profile Send private message
shhac
Expert Cheater
Reputation: 0

Joined: 30 Oct 2007
Posts: 108

PostPosted: Thu Aug 06, 2009 1:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 475

Joined: 09 May 2003
Posts: 25980
Location: The netherlands

PostPosted: Thu Aug 06, 2009 3:50 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
shhac
Expert Cheater
Reputation: 0

Joined: 30 Oct 2007
Posts: 108

PostPosted: Thu Aug 06, 2009 4:10 pm    Post subject: Reply with quote

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
Code:
[0.0088, 0.0154]

//--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
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 475

Joined: 09 May 2003
Posts: 25980
Location: The netherlands

PostPosted: Thu Aug 06, 2009 4:16 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
shhac
Expert Cheater
Reputation: 0

Joined: 30 Oct 2007
Posts: 108

PostPosted: Thu Aug 06, 2009 4:23 pm    Post subject: Reply with quote

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
View user's profile Send private message
DanielG
Expert Cheater
Reputation: 1

Joined: 13 May 2009
Posts: 130
Location: The Netherlands

PostPosted: Thu Aug 06, 2009 6:02 pm    Post subject: Reply with quote

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
View user's profile Send private message
shhac
Expert Cheater
Reputation: 0

Joined: 30 Oct 2007
Posts: 108

PostPosted: Fri Aug 07, 2009 3:20 pm    Post subject: Reply with quote

This was a case-specific thing for a trim function, not a general "anything" question. Thanks for your input though.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites