Python Forum
bigsqrt - Python vs Pike
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bigsqrt - Python vs Pike
#1
this is a commandline program i wrote many years ago in both Python (my first Python program) and Pike. both are given below.  the Pike code is substantially faster.  this program does its work using big ints, so that ends up being what is compared.  give it one argument, the number to find the square root of.  if no arguments are given, it will compute the golden ratio.  it just keeps outputting digits to get more and more precise (never ending).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
#2
What if you import gmpy2 instead of math module?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
#3
Once you hit line 57, your program is done. The python version never computes anything, regardless of arguments, as you unconditionally return out of the main function.
#4
Once that return issue is resolved, I'd be curious about actual benchmarks, including using PyPy instead of CPython (which I imagine you're using).
#5
line 57 should be indented.  it is in my copy of the code which works for me. how can i insert plain code, now?

Output:
lt1/forums /home/forums 3> head -n57 bigsqrt.py|tail -n3     if len(num_parts) != 2:         stderr.write( "Number needs 0 or 1 '.'\n" )         return 1 lt1/forums /home/forums 4>
when i c&p the code to this post editing window (from a terminal window in a different desktop), the "return" line was unindent by 4 spaces so that it was just as far as the "if".  i typed in 4 spaces to fix it (looks right in preview now).  maybe that is why the program code was inserted wrong.

the new code display refuses to scroll when doing a highlight select ... we need a "select all" or better yet, a "download" link or button.  the old way to display code did have a "select all".  a "download" would be better, IMHO.

i put the 2 bigsqrt sources on the website as links to avoid the indentation issues:

http://stratusrelay.com/phil/bigsqrt.py (4f5b52aa9d8043a2d3d9d090f04dec7e)

http://stratusrelay.com/phil/bigsqrt.pike (3928561f6465d036852701a7a2421b21)
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
#6
Quote:i put the 2 bigsqrt sources on the website as links to avoid the indentation issues:

It looks more like a problem in the way you store code on web.
I save bigsqrt.py and then try to open it PyCharm,Atom,NotePad++ and indentation is wrong in all.
The same happen with copy-paste from website.
#7
Since we're still talking about line 57, it looks like the "if" clause line is indented with spaces, while the body of that same if block is indented with tabs. Mixing the two causes... strange things to happen :p
#8
(Oct-05-2016, 02:30 PM)nilamo Wrote: Since we're still talking about line 57, it looks like the "if" clause line is indented with spaces, while the body of that same if block is indented with tabs.  Mixing the two causes... strange things to happen :p

yes, that could be causing it ... anyone got a good untabify script?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
#9
Most text editors can convert back and forth.  Otherwise, it should be as simple as:
with open('python-file.py', 'r') as source:
    with open('python-file-fixed.py', 'w') as out:
        for line in source:
            if line.startswith('\t'):
                line = ' '*4 + line[1:]
            print(line, end='', file=out)
#10
emacs is the only editor i know (besides tools like sed) and it has don untabify wrong before so i want to avoid that.

i tried that code:
Output:
lt1/forums /home/forums 6> py3 untab.py bigsqrt.py fixed.py Traceback (most recent call last):   File "untab.py", line 13, in <module>     main()   File "untab.py", line 7, in main     for line in source:   File "/usr/lib/python3.5/encodings/ascii.py", line 26, in decode     return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xa9 in position 138: ordinal not in range(128) lt1/forums /home/forums 7> cat untab.py def main():     from sys import argv     fn = argv[1]     tf = argv[2]     with open(fn, 'r') as source:         with open(tf, 'w') as out:             for line in source:                 if line.startswith('\t'):                     line = ' '*4 + line[1:]                 print(line, end='', file=out)     return main()
google tells me about the GNU expand command which i found is already in Ubuntu 16.04.1 LTS :)  it seems to work so i will attach the fixed file.  the attach system here does not like .pike so an untabify of that is not attached.

Attached Files

.py   bigsqrt.py (Size: 4.75 KB / Downloads: 4)
Tradition is peer pressure from dead people

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


Forum Jump:

User Panel Messages

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