<div dir="ltr">I agree with <span style="font-size:12.8px">Dennis.</span><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Unless your profiler point this set code got so many percentage spent on, I will not look at the code to make it faster. If you really want to make code faster, there is cuda if you want to take advantage of your nvidia graphics card or simd for your cpu :-) There is not much waste in delphi convert to assembly or write directly in assembly code to get it faster I guess.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Regards</span></div><div><span style="font-size:12.8px">Leigh</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 9 June 2016 at 07:56, Dennis Chuah <span dir="ltr"><<a href="mailto:dennis_cs_chuah@hotmail.com" target="_blank">dennis_cs_chuah@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Personally, I wouldn't bother optimising my code at this level. You might<br>
save a few micro seconds, but consider the bigger picture. There are lots of<br>
other things your program can do that are potentially more time and resource<br>
consuming, eg. db access, disk access, network access, algorithms.<br>
<br>
Run a profiler and find out where your program is inefficient. Consider<br>
using worker threads to run lengthy operations.<br>
<br>
As for the answer to your original question of which is more efficient ...<br>
should you really care? Perhaps what is more important is code readability.<br>
IMHO, something like<br>
<br>
a in [1..3, 5]<br>
<br>
is more readable (and fewer keys to type) than<br>
<br>
((a >= 3) and (a <= 5)) or (a = 5)<br>
<br>
BUT<br>
<br>
type<br>
TSomeKind = (<br>
skSomeKind = 1,<br>
skAnotherKind = 2,<br>
skYetAnotherKind = 3,<br>
skYouGuessedIt = 4,<br>
skFinally = 5<br>
);<br>
<br>
a in [skSomeKind, skAnotherKind, skYetanotherKind, skFinally]<br>
<br>
perhaps better conveys the meaning of the code, assuming the integers 1..5<br>
have special meaning in your program.<br>
<br>
Then, if you only had those enumerations to care about, your condition<br>
reduces to<br>
<br>
a <> skYouGuessedIt<br>
<br>
which is much more readable, and I am sure more efficient than the other<br>
constructs above, if efficiency at this level is that important to you.<br>
<br>
Regards,<br>
Dennis.<br>
<br>
From: Ross Levis<br>
Sent: Thursday, June 9, 2016 12:00 AM<br>
To: 'NZ Borland Developers Group - Delphi List'<br>
<div><div class="h5">Subject: Re: [DUG] In [set] efficiency<br>
<br>
<br>
So it’s best to reduce the set size as much as possible. Looking at my<br>
code, I have some variables declared as Word (16-bit) with 65535 possible<br>
values but most are Byte.<br>
<br>
<br>
<br>
<br>
<br>
From: <a href="mailto:delphi-bounces@listserver.123.net.nz">delphi-bounces@listserver.123.net.nz</a><br>
[mailto:<a href="mailto:delphi-bounces@listserver.123.net.nz">delphi-bounces@listserver.123.net.nz</a>] On Behalf Of Peter Ingham<br>
Sent: Wednesday, 8 June 2016 8:15 p.m.<br>
To: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>
Subject: Re: [DUG] In [set] efficiency<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
(From memory & without firing up a compiler to verify) ...<br>
<br>
Sets are implemented as bitmaps with one bit allocated to each possible<br>
value of the set type (hence the limitations on the number of possible<br>
elements in the set).<br>
<br>
So if you have<br>
type bytesset = [0..7]; // 8 possible values<br>
var a: bytesset ;<br>
begin<br>
if a in [1,4] then<br>
<br>
The set literal [1,4] ends up as a byte with the value $0A.<br>
The test "a in [1,4]" is equivalent to "(a AND $0A) <> 0".<br>
<br>
The generated code (for a set of this size) is an AND followed by a<br>
conditional jump. Very efficient.<br>
<br>
It gets a little nastier when the possible values gets larger as the size of<br>
the set grows (set of AnsiChar is 32 bytes; Set of Widechar exceeds the<br>
permissible size of a set).<br>
<br>
The precise code generated by case varies between jump tables and linear<br>
comparisons.<br>
<br>
Regards<br>
<br>
<br>
On 8/06/2016 5:27 p.m., Steve Peacocke wrote:<br>
<br>
<br>
<br>
If I remember correctly, the compiler changes both to array anyway so you<br>
come out with exactly the same compiled code.<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
Perhaps someone can confirm this or tell me how wrong I am?<br>
<br>
Steve Peacocke<br>
<br>
<br>
<br>
+64 220 612-611<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
On 8/06/2016, at 4:56 PM, Ross Levis <<a href="mailto:ross@stationplaylist.com">ross@stationplaylist.com</a>> wrote:<br>
<br>
<br>
<br>
I’m wondering which is more efficient to process...<br>
<br>
<br>
<br>
if (a=1) or (a=2) then ...<br>
<br>
<br>
<br>
or<br>
<br>
<br>
<br>
if a in [1,2] then ...<br>
<br>
<br>
<br>
If the answer is the first method, does it make a difference if more numbers<br>
are checked, eg. if a in [1..3,5] then<br>
<br>
<br>
<br>
Cheers.<br>
<br>
<br>
<br>
_______________________________________________<br>
NZ Borland Developers Group - Delphi mailing list<br>
Post: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>
Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" rel="noreferrer" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with<br>
Subject: unsubscribe<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div></div>_______________________________________________NZ Borland Developers Group -<br>
Delphi mailing listPost: delphi@listserver.123.net.nzAdmin:<br>
<a href="http://delphi.org.nz/mailman/listinfo/delphiUnsubscribe" rel="noreferrer" target="_blank">http://delphi.org.nz/mailman/listinfo/delphiUnsubscribe</a>: send an email to<br>
<div class="HOEnZb"><div class="h5"><a href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe<br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
NZ Borland Developers Group - Delphi mailing list<br>
Post: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>
Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" rel="noreferrer" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with<br>
Subject: unsubscribe<br>
<br>
_______________________________________________<br>
NZ Borland Developers Group - Delphi mailing list<br>
Post: <a href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>
Admin: <a href="http://delphi.org.nz/mailman/listinfo/delphi" rel="noreferrer" target="_blank">http://delphi.org.nz/mailman/listinfo/delphi</a><br>
Unsubscribe: send an email to <a href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe</div></div></blockquote></div><br></div>