Page 1 of 1

Fastest sort

Posted: 15 Jan 2019, 16:52
by mast4rwang
I've read this article about sorting: https://en.wikipedia.org/wiki/Quicksort and I made some sort scripts, some using my own way, some similar to the logic of quicksort but it seems that nothing is faster than the default string sort. Here is a code from https://sites.google.com/site/ahkref/cu ... /sortarray which uses pivots to optimize sorting and still it is several times slower than using the simple method. Why is that and is it possible to sort faster than the default sort if I want to sort only numbers?

Pivoting string sort:
Spoiler

Simple method:

Re: Fastest sort

Posted: 15 Jan 2019, 18:55
by FanaticGuru
A sort truly implemented in AHK is never going to be as fast as a the Sort command which is implemented in C++. The Sort command is probably a Quicksort at its core.

Using the Sort command to sort an array object is mostly the time it takes to convert to a string and then convert back to an array object. The actual Sort command is probably only a fraction of the conversion time.

FG

Re: Fastest sort

Posted: 15 Jan 2019, 20:13
by IMEime
Fast...
Fast is good always.

I have met AHK because it is fast to learn.
I use AHK because it is fast to change code.
I will use AHK because it is fast to learn again when I totally forgot its command.
As you can see I do not have that kind 'fast' in my inventory.

Yes, I know AHK is slow in some case and very slow for some other cases.
If you find some nice way, tell me please, I will check it out.

I have tested fast MS Excel and fast MS Word codes.
Now, trying to test fast SQLite, but its is difficult to me...

Re: Fastest sort

Posted: 15 Jan 2019, 23:32
by coffee
FanaticGuru wrote:
15 Jan 2019, 18:55
A sort truly implemented in AHK is never going to be as fast as a the Sort command which is implemented in C++. The Sort command is probably a Quicksort at its core.
It uses qsort from maykrosoft, so YEET.

mast4rwang wrote:
15 Jan 2019, 16:52

Code: Select all

Array[pivot]
I'm gonna let someone else spoil your fun and tell you what happens when you do that :'(

Re: Fastest sort

Posted: 16 Jan 2019, 15:43
by mast4rwang
coffee wrote:
15 Jan 2019, 23:32
FanaticGuru wrote:
15 Jan 2019, 18:55
A sort truly implemented in AHK is never going to be as fast as a the Sort command which is implemented in C++. The Sort command is probably a Quicksort at its core.
It uses qsort from maykrosoft, so YEET.

mast4rwang wrote:
15 Jan 2019, 16:52

Code: Select all

Array[pivot]
I'm gonna let someone else spoil your fun and tell you what happens when you do that :'(
Let me guess.... It slows down the script? :D
This function is actually really slow even though it is posted on that website and uses pivots. I think my own sketch sorts were faster while testing:
Spoiler
But as FG wrote, if it uses c++ sort I guess there is no chance to surpass the default sort ^^

Re: Fastest sort

Posted: 17 Jan 2019, 13:41
by FanaticGuru
mast4rwang wrote:
16 Jan 2019, 15:43
But as FG wrote, if it uses c++ sort I guess there is no chance to surpass the default sort ^^
I assume the default sort is very good. If it is Qsort by Microsoft then it is used in thousands of programs. I imagine it has been picked over and optimized by many different people.

The only problem is that AHK uses it only for strings. It would be nice if it could sort arrays with variable dimensions where the dimension to sort on as a parameter. Other languages have this.

Ironically AHK is probably converting the string to an array to be sorted in C++.

It would be great if someone implimented this option in AHK through some Mcode or a Dll; or some other way to get a multi-dimensional array sort at C++ speed.

FG

Re: Fastest sort

Posted: 17 Jan 2019, 15:41
by mast4rwang
FanaticGuru wrote:
17 Jan 2019, 13:41
mast4rwang wrote:
16 Jan 2019, 15:43
But as FG wrote, if it uses c++ sort I guess there is no chance to surpass the default sort ^^
It would be great if someone implimented this option in AHK through some Mcode or a Dll; or some other way to get a multi-dimensional array sort at C++ speed.

FG
Yeah and share the code so everyone can help optimize it :thumbup:

Re: Fastest sort

Posted: 18 Jan 2019, 02:41
by coffee
mast4rwang wrote:
16 Jan 2019, 15:43
Let me guess.... It slows down the script? :D
xd, you are just not accessing direct memory addresses like you would in a compiled language (and maybe google's v8? i know they do some fuckery with hidden classes), i.e you are not going straight to the element, you may have to go over a few, in addition to some prechecks before. You are incurring a lookup (currently implemented as a binary search).
Anything like that implemented in autohotkey can only be compared to other autohotkey implementations. You would have to screw up on purpose to come up with a slower sorting, searching or parsing algorithm in c++ compared to something done in autohotkey.

Re: Fastest sort

Posted: 23 Jan 2019, 12:44
by rommmcek
@mast4rwang: I believe that your SortArray() function doesn't respect order of appearance completely (except in special cases).
I made here a bad proposal to improve it, so I deleted it!
However one of my affirmation stands: The SortArray() function isn't the fastest in all occasions!

Re: Fastest sort

Posted: 23 Jan 2019, 13:14
by nnnik