Four different ways to iterate.

About Monkey 2 Forums Monkey 2 Programming Help Four different ways to iterate.

This topic contains 5 replies, has 4 voices, and was last updated by  Jesse 2 years ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #7674

    cocon
    Participant

    This is not a request for help, but rather a snippet. I don’t know if there is a better place to post this but anyway here it is. Having a class that acts as a container of a list – exactly as the standard List<T> but not a list implementation, more as a wrapper of a list – you can iterate it’s contents with four different ways. My favorite option is “D” as you see below because you can essentially trick the API and handle it virtual as a list.

    Updated Code: 08 Apr 2017

    #7774

    therevills
    Participant

    Does any one way have a speed advantage over the others?

    #7783

    cocon
    Participant

    I did a measurement to see for real, time is in millisecs.

     

    Some back story behind this test.
    In real circumstances someone would go for the option A which is the most obvious and well understood concept. So this means that it’s end of story. 🙂 ____ All of the other ways are most close to the concept of internal DSLs (I learnt about it these days) it’s about how you can utilize these techniques to create a very smooth API.  ____ Option B converts your list to array, Option D proves how Monkey can use the To operator effectively to convert from List to List<B>. However option C is my favorite because it provides this smooth API transition, this way you can bypass internal class contents and let the object present itself according to how it is called (To operator in action).

     

    Regarding the benchmarks
    One striking thing to note in these benchmarks is that I never expected that array iteration (B) would be this fast. I had a subconscious fear for this due to this array conversion, (from the point I have a list ready to use – I see no reason to convert it to a list and iterate it) – however as it seems there is some compiler logic that make this happen. As far as I see this conversion happens only once, then the compiler will have to jump straight into array blocks. On the other hand option (A) as it involves objects and pointers, makes accessing the memory much more complicated.

    #7784

    Jesse
    Participant

    here! test E is way faster.

    [/crayon]
    #7801

    impixi
    Participant

    @jesse

    Your TestE function is wrong. Hint: it has something to do with your while condition. 😉

    EDIT: Okay here’s a solution (because Lists in MX2 are apparently circular):

    [/crayon]
    #7803

    Jesse
    Participant

    @impixi you are right. hahaha! At first I didn’t know why it wasn’t working. That’s because I have always used it with objects not integers or floats so I just assumed it was doing what it was supposed to. it works fine with objects not primitives. your solution solves it. I have to keep that in mind. unlikely I will use integers or floats in a list but who knows. Thanks!

    It is still faster but not as I gave it credit for.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.