omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    print is slow

    Pythonista
    2
    4
    1924
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • wolf71
      wolf71 last edited by

      Pythonista using print function is very slow.

      can using other method to improve speed?

      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        Where possible, try to call print() as few times as possible by bundling up a bunch of lines of text into just one call to print().

        Like:

        my_text = '''Triple quoted strings
         allow you to
         create
         strings
         that are
         multiple
         lines long.'''
        
        print(my_text)
        # is faster than
        for line in my_text.splitlines():
            print(line)
        
        # or use str.join()
        import string
        print('\n'.join([c for c in string.lowercase]))
        # is faster than
        for c in string.lowercase:
            print(c)
        
        1 Reply Last reply Reply Quote 0
        • wolf71
          wolf71 last edited by

          thanks @ccc

          s=''
          for i in range(100):
          s += 'test afsdafdsaf\n'
          print s

          it's fast,but sometime I need print split. etc: output process info.

          can speed up every time print output ?

          1 Reply Last reply Reply Quote 0
          • ccc
            ccc last edited by

            Is the text that you a printing out for your end user or is it just for you own debugging purposes? If the latter, consider using logging instead of printing.

            It is also possible to redirect stdout to a file instead of the console which will be much faster but I find that complex/counterintuitive.

            You could also create a ui.TextView and write your text there: text_view.text += 'new message'

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors