« first day (1454 days earlier)      last day (1382 days later) » 

12:51 PM
"Inspired" from this "kata": https://www.codewars.com/kata/55c45be3b2079eccff00010f/train/javascript

Is sorting and managing memory slower than indexing memory?
Example: Is this slower
// array
[3rd, 93rd, 2nd, 1st] // before MMU
[1st, 2nd, 3rd, 93rd] // after MMU and several cycles

foreach (rank in array) print(rank) // print the ranks in order
than this?
array = [3rd, 93rd, 2nd, 1st]

// print the ranks in order
for (index = 0, order = 1st; index != array.length; ++index) {
    rank = array[index];

    if (order == rank) { // if the next rank (in order) has been found
        index = 0; // look for the next rank (in order) by restarting the loop
        order = order -> next; // go the next order i.e.: 1st to 2nd

        print(rank)
    }
}
 
1:23 PM
"Inspired" from this "kata": https://www.codewars.com/kata/55c45be3b2079eccff00010f/train/javascript

Is sorting and managing memory slower than indexing memory?
Example: Is this slower
// array
[3rd, 93rd, 2nd, 1st] // before MMU
[1st, 2nd, 3rd, 93rd] // after MMU and several cycles

foreach (rank in array) print(rank) // print the ranks in order
than this?
array = [3rd, 93rd, 2nd, 1st]

// print the ranks in order
for (index = 0, order = 1st; index != array.length; ++index) {
    rank = array[index];

    if (order == rank) { // if the next rank (in order) has been found
        index = 0; // look for the next rank (in order) by restarting the loop
        order = order -> next; // go the next order i.e.: 1st to 2nd

        print(rank)
    }
}
 
I don't understand order = order -> next; but wouldn't that make it quadratic worst-case ?
But I guess the real answer is "measure and verify" because all the theoretical answers are mostly based around a large number of entries. With a small number of elements the caching effects might give a very different answer
 
@PeterT Time complexity?
@PeterT Had a feeling profiling might be the answer but was also curious about if it was generally faster to index an array in order than to sort it first (and then index it)
(It's pseudo-code as well so I'm not sure if profiling might fully apply to the example)
 
I don't think it's generally the case, because to observe the correct order you need to memorize the elements you already indexed anyway in some way, at that point you might as well sort it. You're not really saving anything
 
Hmm, I guess so.

Looking closer at the indexing code used it does seem like a primitive sort ("look for the next item in order and print it?", that's sorting)
 
plus if you have two elements with the same value you're going to either ignore one of them or you're going to loop forever if you don't store a list of elements with the same value
 
1:37 PM
Lol yea, it was only a simple sorting algorithm. Doesn't even work actually (index = 0 is meant to be index = -1 in it's case)
Anyway, in this case it seems "Sort -> Index -> Print" (method 1) is better than "Index -> Sort -> Print" (method 2)
 

« first day (1454 days earlier)      last day (1382 days later) »