# Is there a more effective (ie. FASTER) was to select from a large list?

**URL:** <https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048>\
**Category:** Other\
**Created:** [29 January 2023 16:09 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048 "2023-01-29T16:09:18Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [29 January 2023 16:09 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/1 "2023-01-29T16:09:18Z")

</div>

I have a custom graphics class (think canvas)… This has one major task and that is to display an image on the screen. This class has a key property with (currently 57 values).  
So based on the value of that key, the canvas will be one of 57 images.

Right now the code looks like this (and it does “work”)

```auto
private func drawIMAGE(imgKey:Int) {
   switch imgKey {
    case 1 : draw_image_1
    case 2 : draw_image_2
    ...
    case 57 : draw_image_57
   }
}

```

followed by

```auto
private func draw_image_1() { ... drawing code }
private func draw_image_2() { ... drawing code }
...
private func draw_image_57() { ... drawing code }

```

So I have a switch (SELECT CASE) statement with 57 conditions, and 57 functions to draw each of the possible images

---

<div class="post-metadata">

**Author:** ![Tim](https://ifnotnil.com/user_avatar/ifnotnil.com/tim/32/13_2.png) [@Tim](https://ifnotnil.com/u/Tim)\
**Post date:** [29 January 2023 16:12 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/2 "2023-01-29T16:12:55Z")

</div>

Why wouldn’t you have a property holding the image to draw and a generalized function that draws that image? Are the functions that draw significantly different?

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [29 January 2023 16:24 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/3 "2023-01-29T16:24:10Z")

</div>

yes the functions are all quite differnet… and a given image might change (become wider, taller, change colors etc)… but it caches the image … so it only recreates it if it changes

each image is somewhat dynamic, think Xojo IDE controls in the designer

---

<div class="post-metadata">

**Author:** ![Tim](https://ifnotnil.com/user_avatar/ifnotnil.com/tim/32/13_2.png) [@Tim](https://ifnotnil.com/u/Tim)\
**Post date:** [29 January 2023 18:21 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/4 "2023-01-29T18:21:06Z")

</div>

Gotcha, I was thinking more along the lines of your card game.

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [29 January 2023 18:22 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/5 "2023-01-29T18:22:17Z")

</div>

No worries… the card game is an indexed array… with static images… different situation 🙂

---

<div class="post-metadata">

**Author:** ![bgrommes](https://ifnotnil.com/user_avatar/ifnotnil.com/bgrommes/32/2651_2.png) [@bgrommes](https://ifnotnil.com/u/bgrommes)\
**Post date:** [29 January 2023 23:29 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/6 "2023-01-29T23:29:02Z")

</div>

The only way I can think of to make this less gnarly and more easily maintained, at some performance expense, would be using reflection (.NET) or introspection (Xojo, Swift). Then you would make the function call after determining the function name from the imgKey (or call a DrawImage() method in a static class whose name is thusly determined, etc).

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [30 January 2023 00:41 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/7 "2023-01-30T00:41:15Z")

</div>

Really depends on how this all works.

For instance instead of storing an imageKey with an object, you can store a NSSelector, then use the sendmessage API to execute that function. I’ve used this technique in several places, but not with this many methods.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 January 2023 02:20 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/8 "2023-01-30T02:20:58Z")

</div>

in Xojo style code

```auto
delayed no_arg_delegate

dim funcs() as no_arg_delegate

```

… now load address of each function into the array ONCE

```auto
Sub loadJumpTable()
    // skip position 0
    funcs.append addressof do_nothing
    funcs.append addressof draw_image_1
    funcs.append addressof draw_image_2
    ...
   funcs.append addressof draw_image_57

end sub

```

and later you can just

```auto
   Sub drawIMAGE( imgKey as integerInt)

         dim a_delegate as no_arg_delegate

         a_delegate = funcs(imgKey)

         a_delegate.invoke
   end sub

```

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [30 January 2023 06:08 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/9 "2023-01-30T06:08:10Z")

</div>

Thanks… that provides something to think about… the question is, will any of those be “faster enough” to make the effort worth while. 🙂

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [30 January 2023 07:26 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/10 "2023-01-30T07:26:49Z")

</div>

I think it would be because you’re avoiding logic gates and the comparison of potentially 57 objects, before the correct function is called.

When working with CIKL, any kind of logic gate **really** slows down shader performance. So you have to get creative.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 January 2023 14:45 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/11 "2023-01-30T14:45:10Z")

</div>

The jump table should be measurably faster since  
It certainly is in Xojo code and many other languages

---

<div class="post-metadata">

**Author:** ![bgrommes](https://ifnotnil.com/user_avatar/ifnotnil.com/bgrommes/32/2651_2.png) [@bgrommes](https://ifnotnil.com/u/bgrommes)\
**Post date:** [30 January 2023 15:45 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/12 "2023-01-30T15:45:46Z")

</div>

I like your solution. Technically you are still making a decision at draw time but it should be considerably faster than a case statement for routing (even if you optimize by putting the most common / likely cases first) and there’s no overhead for late-bound tricks. Even “wasting” an element zero is a good thing because you’re saving a subtraction on each call to draw an image.

---

<div class="post-metadata">

**Author:** ![npalardy](https://ifnotnil.com/user_avatar/ifnotnil.com/npalardy/32/10_2.png) [@npalardy](https://ifnotnil.com/u/npalardy)\
**Post date:** [30 January 2023 15:50 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/13 "2023-01-30T15:50:09Z")

</div>

Yeah jump tables are immensely useful esp where you can precalculate values ahead of time and then just directly index into a tablee of values

It was one of many q’s asked many many years ago in interviews for coding  
Write a function to turn all ASCII characters into their upper case variant

1. use whatever code you want
2. no if statements (fine you change to a switch)
3. no if, select case, switch etc \<\<\<\< JUMP TABLE !!!

---

<div class="post-metadata">

**Author:** ![DaveS](https://ifnotnil.com/user_avatar/ifnotnil.com/daves/32/79_2.png) [@DaveS](https://ifnotnil.com/u/DaveS)\
**Post date:** [30 January 2023 16:20 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/14 "2023-01-30T16:20:21Z")

</div>

Seems to implement such a thing in Swift requires ObjC code bound to Swift, and it is not a recommended practice, since Swift considers the use of pointers to be “unsafe”

---

<div class="post-metadata">

**Author:** ![samRowlands](https://ifnotnil.com/user_avatar/ifnotnil.com/samrowlands/32/121_2.png) [@samRowlands](https://ifnotnil.com/u/samRowlands)\
**Post date:** [31 January 2023 00:51 UTC](https://ifnotnil.com/t/is-there-a-more-effective-ie-faster-was-to-select-from-a-large-list/3048/15 "2023-01-31T00:51:12Z")

</div>

Sigh… yeah the msgSend API is Objective-C… [Apple Developer Documentation](https://developer.apple.com/documentation/objectivec/1456712-objc_msgsend)

I found this article, I don’t think it helps, but I’ll share it incase it does inspire something. [Objective-C vs Swift messages&nbsp;dispatch](https://www.untitledkingdom.com/blog/objective-c-vs-swift-messages-dispatch)
