+ 1

Speed up this code?

How can I speed up this code? https://code.sololearn.com/ceWBQY0eYyJw/?ref=app I know I can probably skip the numbers the code has already seen, for example, 16. 16 appears at least twice in the output, so we don't need to do it for each occurrence of the number. But I'm not sure how to do that.

23rd Mar 2019, 12:49 PM
Daniel Cooper
Daniel Cooper - avatar
16 Respostas
+ 7
Put the whole thing in a function so that the compiler can optimize it. Use a static map object to cache the results so that numbers don't have to be calculated several times. http://www.cplusplus.com/reference/map/map/
23rd Mar 2019, 2:01 PM
Anna
Anna - avatar
+ 6
Louis This takes like 0.5 seconds. Seriously? 😅
24th Mar 2019, 6:24 AM
Anna
Anna - avatar
+ 5
This one will take about 14 seconds on a PC to check the numbers up to one million. It's probably terribly written because I changed plans several times while writing it. There's a lot of room for improvement :( https://code.sololearn.com/c0iNaKcUnLLj
23rd Mar 2019, 6:55 PM
Anna
Anna - avatar
+ 4
Dynamic programming. I dont know cpp that well, but in python I would build a dictionary that contains all the answers as you calculate them. When you encounter a value you have already seen in a previous run, you copy the values from the dictionary instead of repeating the same calculations. Edit: this is not applicable to this question since the outcome is just the value and lentgth of longest chain, not all values.
23rd Mar 2019, 12:59 PM
Louis
Louis - avatar
+ 4
Next attempt: down to 7 seconds https://code.sololearn.com/cmUn96v59Yya/?ref=app
23rd Mar 2019, 8:01 PM
Anna
Anna - avatar
+ 4
I replaced i = i*3+1 with i=(i<<1)+i+1 and i /= 2 with i >>= 1, doesn't seem to affect the speed though 😞
23rd Mar 2019, 8:13 PM
Anna
Anna - avatar
+ 4
I think I'll rewrite the whole thing tomorrow 😓 The already calculated results are stored in seq_len though
23rd Mar 2019, 8:27 PM
Anna
Anna - avatar
24th Mar 2019, 5:02 AM
Louis
Louis - avatar
+ 3
Is there a time limitation? I wrote a code and it seems to be working, but it's somehow noticeably slower than a python script 😅
23rd Mar 2019, 6:09 PM
Anna
Anna - avatar
24th Mar 2019, 5:57 AM
Louis
Louis - avatar
+ 2
maybe replacing n=n*3+1 by n+=(n<<1)+1 and n=n/2 by n>>=1 could be faster. It will depend on how the compiler optimizes the code. In assembler my way is way faster than multiplying or dividing integers (something like 1 instruction against 1500+ instructions).
23rd Mar 2019, 8:05 PM
Edward
Edward - avatar
+ 1
Anna Can you give me an example of how I would do that? I can't figure it out. I have pretty much figured out how to calculate and count the terms in the sequence, but my method would take a while xD I'm not sure if you're familiar with Project Euler Problem #14, but I can explain it if you need me to.
23rd Mar 2019, 4:37 PM
Daniel Cooper
Daniel Cooper - avatar
+ 1
Nope. Won't be running it via code playground. Lol
23rd Mar 2019, 6:46 PM
Daniel Cooper
Daniel Cooper - avatar
+ 1
Anna so probably your compiler is optimizing the code in a decent way. My comment is mainly valid when you lack of a math coprocessor (pre-pentium era), and must use a crappy 8 bit processor (i.e. embedded) were you must implement multiplication and division in software ;) Your seq_len is already your linked list.
23rd Mar 2019, 8:40 PM
Edward
Edward - avatar
0
And the response is clearly a linked list. You could work using a linked list to store prior results and retrieve any result already calculated.
23rd Mar 2019, 8:24 PM
Edward
Edward - avatar
0
Empirical confirmation that odds are longer than evens. https://code.sololearn.com/c75K2ELgBZQ0/?ref=app
24th Mar 2019, 8:28 AM
Louis
Louis - avatar