Python Forum
numeric string sort - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: numeric string sort (/thread-7692.html)



numeric string sort - Skaperen - Jan-21-2018

the numeric string sort is a sort based on numeric string compare. this compare works like a normal string compare until it encounters numeric digits. if it is comparing a digit to a non-digit, it still compares like a normal string compare, comparing the individual characters of each string to be compared. if it encounters digits in both strings it is comparing at the same time, then the behavior is changed. it scans forward in both strings to get the number of digits in a run. if the two digit runs are different, then the difference number of digits are scanned on the longer run to see if any digits are non-zero. if so then the longer run is considered to be the higher value. otherwise the remaining equal number of digits in both runs are compared to determine how the strings compare.

i have done this in C and the performance was somewhat less than a normal compare since it did not get to use machine specific optimization in the library compare and compiler optimization was less as well. many CPU architectures, such as IBM mainfram S/360, S/370, S/390, have CPU instructions to do a wholes compare, but not for a numeric string compare.

in Python, the performance comparison, i expect, would be even more exaggerated since an implementation of everything from comparison to sorting would all be in Python code. to make an implementation be even better, i suspect this would need to be done in C. have you seen such a thing available?


RE: numeric string sort - wavic - Jan-21-2018

You may try Nuitka. It translates Python code to C code and compiles it. As they say. I didn't need it yet so I didn't try it.


RE: numeric string sort - Skaperen - Jan-22-2018

this looks useful for a few of my scripts. i think i will try it on my program that calculates a growing string of digits for the square root of a number. but Pike readily wins in this case, because Pike uses libgmp.


RE: numeric string sort - Gribouillis - Jan-22-2018

(Jan-22-2018, 04:08 AM)Skaperen Wrote: Pike uses libgmp.
You can use libgmp from python too.


RE: numeric string sort - Skaperen - Jan-23-2018

(Jan-22-2018, 07:03 AM)Gribouillis Wrote:
(Jan-22-2018, 04:08 AM)Skaperen Wrote: Pike uses libgmp.
You can use libgmp from python too.
will it apply itself to language expressions and operators so i can still use code like foo = 2**2000 + 2**1000 and have libgmp do it, or do i need to code libgmp calls to use it?

bigsqrt.pike

bigsqrt.py

i would not want to change all that code to a bunch of library calls. the operators are easier to read.


RE: numeric string sort - Gribouillis - Jan-23-2018

(Jan-23-2018, 02:38 AM)Skaperen Wrote: will it apply itself to language expressions and operators
I think the answer is yes for a good part of it, but you'll need to use the libraries constructors:
>>> x = gmpy2.mpz('2')
>>> x**2000 + x**1000
mpz(11481306952742545242328332011776819840223177020886952004776427...) # I removed some numbers here



RE: numeric string sort - Skaperen - Jan-23-2018

so how much of the code of bigsqrt.py would need to be changed to use gmpy? just the literals?