which method more faster?

Forum closed. All further discussion to be discussed at https://github.com/OpenKore/

Moderator: Moderators

ezza
Developers
Developers
Posts: 109
Joined: 04 Apr 2008, 09:50

which method more faster?

#1 Post by ezza »

just asking...

given $b is a random int number from $a to $c... so which method more faster?

1.

Code: Select all

      foreach ($a .. $c) { return 1 if $_ == int($b) }
or

2.

Code: Select all

     return 1 if $a <= $b && $b <= $c


thanx in advance
sli
Perl Monk
Perl Monk
Posts: 810
Joined: 04 Apr 2008, 17:26
Noob?: No

Re: which method more faster?

#2 Post by sli »

I hate to say something like this to a fellow staff member, but... If you really have to ask, you need to work on your programming fundamentals. Loops will always be slower. Allow me to explain:

The second one is a single if-statement, but the loop will run exactly $b times before returning. So if $a is 1, $b is 9999, and $c is 10000, the loop will run 9,999 times before terminating. That said, I highly doubt the difference will be noticeable unless you expect $c to be a large number at any point. The second one is more readable as well.
cs : ee : realist
ezza
Developers
Developers
Posts: 109
Joined: 04 Apr 2008, 09:50

Re: which method more faster?

#3 Post by ezza »

sli wrote:I hate to say something like this to a fellow staff member, but... If you really have to ask, you need to work on your programming fundamentals. Loops will always be slower. Allow me to explain:

The second one is a single if-statement, but the loop will run exactly $b times before returning. So if $a is 1, $b is 9999, and $c is 10000, the loop will run 9,999 times before terminating. That said, I highly doubt the difference will be noticeable unless you expect $c to be a large number at any point. The second one is more readable as well.


Yes I'm one of our staff members... but i'm no programmer or received any programming lesson. So, I dont quite see if there should be any flame/s needed as I think I posted my doubt in the correct way and in the appropriate place(i think so).

Anyway, I appreciate your explaination on the matter. Now, I have about 95% confident saying that I should go for 2nd option... the ress 5% goes to your highlighted statement.


p/s: @sli you are always the man :)


Related Topic: Enable: Range Numbers in Auto/macro - v2.0.2
sli
Perl Monk
Perl Monk
Posts: 810
Joined: 04 Apr 2008, 17:26
Noob?: No

Re: which method more faster?

#4 Post by sli »

Oh, I figured you were a programmer. :lol: At least you learned something today!
cs : ee : realist
kali
OpenKore Monk
OpenKore Monk
Posts: 457
Joined: 04 Apr 2008, 10:10

Re: which method more faster?

#5 Post by kali »

Like ezza, I never had any formal education in computer science (and yet I work as a software developer ... duh) but isn't the second line simply testing if $b is between $a and $c ? The first does the same thing too, but iterates over the whole sequence.

So I guess the second line runs exactly twice (once if you can do short circuiting half of the time, if $b is indeed a random number between $a and $c) while the first runs in constant time $b.
Got your topic trashed by a mod?

Trashing topics is one click, and moving a topic to its proper forum is a lot harder. You expend the least effort in deciding where to post, mods expend the least effort by trashing.

Have a nice day.
sli
Perl Monk
Perl Monk
Posts: 810
Joined: 04 Apr 2008, 17:26
Noob?: No

Re: which method more faster?

#6 Post by sli »

The fancy, computer science term would be O$b. That "O" should actually be a math symbol, but the symbol looks just like an "O."
cs : ee : realist
ezza
Developers
Developers
Posts: 109
Joined: 04 Apr 2008, 09:50

Re: which method more faster?

#7 Post by ezza »

kali wrote:So I guess the second line runs exactly twice (once if you can do short circuiting half of the time, if $b is indeed a random number between $a and $c) while the first runs in constant time $b.

:shock: Goshh... kali! now i'm back at the 50-50 status when i read back sli post and compare to your post. I dont wana think about it anymore... could you two just point me which method I should use in the the link given? Is it if statement or foreach loop?


p.s: Its a headache :)
kali
OpenKore Monk
OpenKore Monk
Posts: 457
Joined: 04 Apr 2008, 10:10

Re: which method more faster?

#8 Post by kali »

Do the one that is simpler. I'm not actually contradicting sli's post :)

What I usually do is to write some pseudocode to see if my actual code is understandable. In your case for example, I want to check if $b is between $a or $c so I ask myself: when is a number between two numbers? My answer would be when it is greater than the lower number, but less than the higher number; i.e.:
a < b < c

So you can formulate your code like:
return 1 if $a < $b and $b < $c;

But I'd agree with sli - if $b had small enough values, it doesn't make much of a difference. Use whatever is more readable and will show your intent. For instance, if you're working with some other objects that you cannot compare with each other, you'd be better off with doing a foreach loop to test if the object is within a range or collection.
Got your topic trashed by a mod?

Trashing topics is one click, and moving a topic to its proper forum is a lot harder. You expend the least effort in deciding where to post, mods expend the least effort by trashing.

Have a nice day.
ezza
Developers
Developers
Posts: 109
Joined: 04 Apr 2008, 09:50

Re: which method more faster?

#9 Post by ezza »

@kali and sli - For any possible number/s ... ofcoz I would rather use the 2nd option (if statement) which is very simple to read. But the headache came after i've read VCL post somewhere in our previous forums regarding the foreach loop... since then, I dont have any confident at all.


Anyway, million thanks to both of you. It really opened up abit my way of coding style. I've edited the enabling range numbers in the macro plugins like the given link in 1st post... should you found any incorrect or miss-coding, it would be great if you could advice me. Thanks again :)