-
-
Notifications
You must be signed in to change notification settings - Fork 19.6k
Use Quick Sort instead of Bubble Sort Algorithm #28069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: bugfix-2.1.x
Are you sure you want to change the base?
Use Quick Sort instead of Bubble Sort Algorithm #28069
Conversation
is your benchmark in the actual board? |
SD sorting is demand-by-user task which is not performance critical. Screen update once in a second and tools like Octoprint don't repeatedly request file list at all. However, increasing use of flash and, especially, RAM is very much undesirable. |
I submitted issue #28064 which this came from. User wait time isn't the issue, but the sort time is causing the watchdog timer to expire forcing a continuous reboot on power up. (more details can be read above) I'm responsible for being critical about the bubblesort. Bubblesort = bad was pounded into me throughout university and I guess it stuck. Using a better sort vs additional RAM use is a tough discussion. If it was just for user experience, I'd say save the RAM. |
Microcontroller programming is specialized. You always have to consider the memory and flash usage as both are a very limited resource as are cpu cycles. If you want to add an option to do a quick sort that is fine, but it has to be optional so those that do not want to allocate resources to it can stick to bubble sort. |
totally agree.
@dbuezas no this was taken from a quick script I wrote up to test/compare the two sorts using the code. I suppose what I can do make an actual test and log the results somehow... unless I am able to make this happen in Simulator. I'm able to load a bunch of .gcode in an .img file, but will this work in the simulator? |
@TheAndr0id thats understandable, Its possible to optimize the code in some way. ~70bytes wont really make or break it seeing that there arent any conflicts with the test configs |
updated code now uses same exact memory/flash like theres no changes |
heres some more stuff to consider
|
You could measure the actual time it takes with millis() and then writing to serial. |
so far I did try that, not sure exactly where to put, results are inconclusive or similar enough. |
Most important thing is to keep the RAM usage from increasing, and if Quick Sort is always faster and always uses no extra RAM, then we can just replace the bubble sort entirely with quick sort. |
3baf8e6
to
3fa2937
Compare
3fa2937
to
c124749
Compare
Well, originally it did use exactly 72bytes more, then I revised it and it made no change, void CardReader::_quicksort(uint8_t* arr, int16_t low, int16_t high) {
if (low < high) {
int16_t pi = _partition(arr, low, high);
_quicksort(arr, low, pi - 1);
_quicksort(arr, pi + 1, high);
}
} in - The recursive quick sort has been replaced with an iterative version to avoid potential stack overflows, which resulted in an extra 72bytes oddly enough. |
error: assignment of read-only reference 'v' if (n < v) v = n; warning: suggest parentheses around comparison in operand of '&' [-Wparentheses] if (i & 0x7 == 7) hal.watchdog_refresh();
i see it was void CardReader::presort() {
...
const int16_t fileCnt = get_num_items(); and that gave an error with |
Recursion will end up using more ram at runtime. |
I'd suggest putting your energy into something actually useful, like implementing sorting by modification timestamp, instead of making changes for the sake of changes. |
well it isn't for that, but for performance and optimization, and who knows may actually end up being better in the long run. but I did explicitly state "I'm not sure how this will do" |
how often you run file sorting? Operations that use sorted list take longer than sorting itself, so, again, optimizing it is very moot. We aren't sorting millions of names to make difference even remotely noticeable. So, since it is infrequently used operation, minimizing flash and ram(both static allocation and runtime stack use) use is priority.
For screen it is already selected via defines - either sorting by name or by place in FAT. |
so far that difference is minute. no difference in ram, barely any for flash |
I wanna say this is ready for review to merge. I can't comment on an actual performance comparison, but you can just lookup that Quick sort is much better in ways than Bubble sort regarding that. if someone can whip up some code that spews results to terminal i can probably test on a real machine. Simulator doesn't do it justice. |
Description
Untested, but the aim is to use Quick Sort instead of Bubble Sort for SD card sorting algorithm
I'm not sure how this will do, but I know it is more efficient at sorting and only uses about 72 more bytes
Requirements
Benefits
Configurations
Related Issues
Comparing them