Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #6233
| From | BartC <bc@freeuk.com> |
|---|---|
| Newsgroups | comp.programming |
| Subject | Re: Back to Basics - IF statements |
| Date | 2016-01-19 13:11 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <n7lchj$jkh$1@dont-email.me> (permalink) |
| References | <5fb53de5-a057-493f-b58c-5c99572513aa@googlegroups.com> |
On 18/01/2016 15:18, mack.altmaniii@gmail.com wrote:
> if (id = 1) {
> set $piece(^|"app"|global("index"),"^",1)=$horolog
> }
> if (id = 2) {
> set $piece(^|"app"|global("index"),"^",3)=$horolog
> }
> if (id = 3) {
> set $piece(^|"app"|global("index"),"^",5)=$horolog
> }
> if (id = 4) {
> set $piece(^|"app"|global("index"),"^",7)=$horolog
> }
> if (id = 5) {
> set $piece(^|"app"|global("index"),"^",9)=$horolog
> }
>
> I feel each pass through the above wastes performance fivefold.
Not necessarily, assuming the 'set $piece' assignment takes much longer
to execute than 'if (id=N)'.
Assuming id is 1 to 5, it will always do five comparisons and one
assignment. Up to four of those comparisons will be unnecessary.
> if (id = 1) {
> set $piece(^|"app"|global("index"),"^",1)=$horolog
> } elseif (id = 2) {
> set $piece(^|"app"|global("index"),"^",3)=$horolog
> } elseif (id = 3) {
> set $piece(^|"app"|global("index"),"^",5)=$horolog
> } elseif (id = 4) {
> set $piece(^|"app"|global("index"),"^",7)=$horolog
> } elseif (id = 5) {
> set $piece(^|"app"|global("index"),"^",9)=$horolog
> }
This will do up to 5 comparisons, and one assignment. There are no
unnecessary ones. Although, if you /know/ that id will be 1 to 5, then
that last 'elseif (id=5)' can just be 'else'. Now it will only do up to
4 comparisons.
If you additionally know that some values of id are more common that
others, you can arrange to test for those values first.
Of course, you can also write the whole thing as one assignment with
'id' as a parameter as others have shown. But since you've already gone
to the trouble of writing this out...
--
Bartc
Back to comp.programming | Previous | Next — Previous in thread | Find similar | Unroll thread
Back to Basics - IF statements mack.altmaniii@gmail.com - 2016-01-18 07:18 -0800
Re: Back to Basics - IF statements Richard Heathfield <rjh@cpax.org.uk> - 2016-01-18 15:24 +0000
Re: Back to Basics - IF statements Kaz Kylheku <kaz@kylheku.com> - 2016-01-18 23:29 +0000
Re: Back to Basics - IF statements LinuxNext <nobody@nospam.net> - 2016-01-18 20:34 -0800
Re: Back to Basics - IF statements Richard Heathfield <rjh@cpax.org.uk> - 2016-01-19 07:14 +0000
Re: Back to Basics - IF statements Jim Lee_0 <jlee54@gmail.com> - 2016-01-21 00:44 -0800
Re: Back to Basics - IF statements Öö Tiib <ootiib@hot.ee> - 2016-01-21 08:53 -0800
Re: Back to Basics - IF statements John McCue <jmccue@jmcnet1.bstnma.east.verizon.net> - 2016-01-18 22:26 +0000
Re: Back to Basics - IF statements Richard Heathfield <rjh@cpax.org.uk> - 2016-01-18 22:33 +0000
Re: Back to Basics - IF statements BartC <bc@freeuk.com> - 2016-01-19 13:11 +0000
csiph-web