-
• #2
Wow, thanks! it's great to see the benchmarks for this. If I'm honest it's a surprise that
cks
is better thancks4
. It's been a real struggle to find anything that's faster, but:// just removing whitespace from the cks callback gives you a tiny bit function cks9(m) { var s = 0; new Uint8Array(E.toArrayBuffer(m)).forEach(function (c) {s^=c}); return s; } // and pre-binding the function and using a for in loop helps a surprising amount function cks10(m) { var s = 0; var c = m.charCodeAt.bind(m); for (var i in m)s^=c(i); return s; }
But you should be able to use
compiled
, for instance try:function xor(a,b) { "compiled"; return a^b; } function cks5(m) { return new Uint8Array(E.toArrayBuffer(m)).reduce(xor, 0); } function cks6(m) { "compiled"; var ck = 0; var l = 0|m.length; for (var i=0;i<l;i++) ck ^= m.charCodeAt(i); return ck; } function cks7(m) { "compiled"; var ck = 0; var l = 0|m.length; // ensure we don't have to check the length each time var c = m.charCodeAt.bind(m); // ensure we don't have to look up 'charCodeAt' each time for (var i=0;i<l;i++) ck ^= c(i); return ck; }
I just had a quick play and they seem to be 2x or more faster than
cks10
(especially the last one). Hope that helps! -
• #3
@Gordon, thanks for your time.
I am trying it now and just face some errors as compiler server seems to be unjoinable.
However, without compiling, cks10 is 16% faster than cks which was my best result.It is a permanent surprise to see how efficiency can be improved on such a language as javascript.
So, let's hope the compiler will be accessible soon.
-
• #4
Give it a try now - I have no idea what happened there!
-
• #5
Here are the results on my Espruino board 1v4:
Mean execution time of nmea_checksum is 14.92417830008 ms,
Mean execution time of cks is 12.01456811692 ms,
Mean execution time of cks3 is 14.42750400967 ms,
Mean execution time of cks4 is 13.73525796113 ms,
Mean execution time of cks9 is 11.70109007093 ms,
Mean execution time of cks10 is 9.84849823845 ms,
Mean execution time of cks5 is 4.01897854275 ms,
Mean execution time of cks6 is 5.00975432219 ms,
Mean execution time of cks7 is 2.71540111965 msSo finally, we have a benefit of 442 % in speed which is obviously reasonnable.
I joined the full source for this comparison.Thank's again.
1 Attachment
-
• #6
Great! I'd still be happier if Espruino executed it more quickly - I'll have to try some benchmarks and see if there's any way to speed up the interpreter a bit more.
Hi all,
I have been analysing the nmea sentences send from a gps module (ublox neo-6m).
What I ended up with was that checking properly those sentences required to compute the xor of all chars.
So finally, it means looping over a string and that takes 55%, approximately 14ms, of the total time required to handle the sentence which takes 25 ms.
I tried various minifications/looping approaches and the code below show the results
Here are the results
So, is there a way to speed up it?
"compiled" javascript doesn't seem to be possible in this context.