By looking at the ETA, I suspect it will take well over 3 days to finish all 10 million moves!
I also move the determination of n_cups out of the loop online loans for Rhode Island, and instead provide it as a parameter of the do_move() function, but it doesn’t really affect the running time either
My guess is that it’s taking so long because of inserting three elements into the vector, which has to make a copy of all the elements that come after the insertion. I am fairly bad at data structures, but I suspect that this might be one of the rare cases where it’s advantageous to use a linked list, since that has quick insertion in the middle. However, before I rewrite the whole thing I change the wasteful 3? insert, to split the vector at the insertion point, and append the three moved cups before sticking the second half of the vector back on:
Rust’s standard library does include a linked list, but it’s a doubly linked list and doesn’t have APIs for detaching and inserting whole ranges, as far as I can tell.
I wonder if, before resorting to rewriting the program with a different linked list data structure from a package, I should profile the existing program to find out what is taking so long. I suspect the insert, but it could be something else, like searching the vector for the destination cup.
I google “rust profiling tools” and land first on cargo-profiler. It looks really easy to install and use, but unfortunately it crashes if you have a digit in the path of your program:
This bug is in a giant-ass regular expression that doesn’t give me a lot of confidence in the robustness this tool. On to the next one.
The cpuprofiler package has pretty decent getting-started documentation. It requires gperftools , so I first install my Linux distribution’s gperftools package, then add this preamble to my program:
I change the number of moves to 1000 and run the program. Read More