Python Forum
bigsqrt - Python vs Pike - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: bigsqrt - Python vs Pike (/thread-280.html)



bigsqrt - Python vs Pike - Skaperen - Oct-04-2016

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).



RE: bigsqrt - Python vs Pike - wavic - Oct-04-2016

What if you import gmpy2 instead of math module?


RE: bigsqrt - Python vs Pike - nilamo - Oct-04-2016

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.


RE: bigsqrt - Python vs Pike - micseydel - Oct-04-2016

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).


RE: bigsqrt - Python vs Pike - Skaperen - Oct-05-2016

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)


RE: bigsqrt - Python vs Pike - snippsat - Oct-05-2016

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.


RE: bigsqrt - Python vs Pike - nilamo - Oct-05-2016

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


RE: bigsqrt - Python vs Pike - Skaperen - Oct-06-2016

(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?


RE: bigsqrt - Python vs Pike - nilamo - Oct-06-2016

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)



RE: bigsqrt - Python vs Pike - Skaperen - Oct-07-2016

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.