Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
A quickie in Typescript
Each digit is enumerated in turn by skimming off the ones digit off each number (using num%10, then dividing the number by 10) until there are no more digits.
The accumulated sum is simply a string with each sum added to its left-hand side.
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
Benchmark here -- numeric method has it by a nose most of the time you run it, sometimes a bit more. For smaller inputs they're pretty much even.
In C or Rust or something closer to the metal Mihail's post is definitely the way to go (imo).
In JS strings are pretty quick and numbers are surprisingly slow to deal with (there's no support for integers, etc) and I find the language doesn't really reward that kind of close-knit optimization, so I felt it wasn't worth the additional complication.
I wonder what the result with asm.js would be.
There are some claims that in the process of implementing asm.js V8 in particular just optimized normal code to the speed of corresponding asm.js, but some extra assertions might still help?
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A quickie in Typescript
Each digit is enumerated in turn by skimming off the ones digit off each number (using
num%10
, then dividing the number by 10) until there are no more digits.The accumulated sum is simply a string with each sum added to its left-hand side.
Testn
You already did the skimming, but you could also avoid stringification altogether: dev.to/qm3ster/comment/14en6
Thanks, that's a fair point. Here's the tweaked code:
Benchmark here -- numeric method has it by a nose most of the time you run it, sometimes a bit more. For smaller inputs they're pretty much even.
In C or Rust or something closer to the metal Mihail's post is definitely the way to go (imo).
In JS strings are pretty quick and numbers are surprisingly slow to deal with (there's no support for integers, etc) and I find the language doesn't really reward that kind of close-knit optimization, so I felt it wasn't worth the additional complication.
I wonder what the result with asm.js would be.
There are some claims that in the process of implementing asm.js V8 in particular just optimized normal code to the speed of corresponding asm.js, but some extra assertions might still help?