2

Considering the following code:

from markdown import markdown

f = open('myfile.md', 'r')
html_text = markdown(f.read())
f.close()

Is there any speed advantage or disadvantage to using io.BytesIO and markdownFromFile? Or is it a wash?

from markdown import markdownFromFile
from io import BytesIO

s = BytesIO()
markdownFromFile(input='myfile.md', output=s)
html_text = s.getvalue()
s.close()

Thanks in advance for any information.

FlipperPA
  • 13,607
  • 4
  • 39
  • 71

1 Answers1

3

It would be best if you benchmark it yourself, but just from the looks of it I don't see any advantage of using BytesIO. Instead of reading the file and parsing it directly into a string, you'd be first reading and processing it into a BytesIO object and then using BytesIO.getvalue to get the string you need.

The former also is easier to read. Could be made even simpler with:

with open('myfile.md', 'r') as f:
    html_text = markdown(f.read())
sirfz
  • 4,097
  • 23
  • 37
  • Thank you! I'll benchmark tomorrow for my own edification, but your answer makes complete sense. – FlipperPA May 25 '15 at 15:02
  • As the developer of Python-Markdown, I would completely agree with this answer with one exception: be careful, you appear to think you are working with bytes; however the Markdown library it all Unicode (both in and out). Of course ASCII input won't be a problem, but anything else will if you don't account for that. – Waylan May 25 '15 at 21:00
  • Thanks for the tip, and all of your work on the excellent library, Waylan - much appreciated. – FlipperPA May 26 '15 at 14:11