Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numeric string sort
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Jan-22-2018, 04:08 AM)Skaperen Wrote: Pike uses libgmp.
You can use libgmp from python too.
Reply
#5
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(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
Reply
#7
so how much of the code of bigsqrt.py would need to be changed to use gmpy? just the literals?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mixed types of numeric data in the statistics module stevendaprano 2 1,302 May-23-2022, 02:15 AM
Last Post: stevendaprano

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020