-
Notifications
You must be signed in to change notification settings - Fork 527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
performance improvements #60
base: master
Are you sure you want to change the base?
Conversation
With this speedup, when you have various pages with png images, the improvements is significant. As a example, I used an old dual core machine, with 104 png images, to generate a pdf, 1 image per page. The time consuming before was 1 minute and 28 seconds. After this patch, the time is 6 seconds!!! Yes, 6 seconds, 14,7x faster!
fpdf/fpdf.py
Outdated
if PY3K: | ||
if not isinstance(s, bytes): | ||
s = s.encode('latin1') | ||
s += b"\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the readme, this supports Python 2.5, which I suspect would choke compiling this syntax
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had no idea about 2.5 here. I have app using this in py2.7 and py3.4. Do you have a suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I am right about s always being str at this point, change it to
s += "\n"
if(self.state == 2):
self.pages[self.page]["content"] += s
else:
if PY3K:
s = s.encode('latin1')
self.stream.write(s)
Otherwise, maybe you could use the b() function from the py3k module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are right!
The general idea sounds good to me. Writing into BytesIO or similar should be well optimized, and concatenating strings in Python performs badly in this sort of scenario. |
@reingart: This patch is good to be merged? |
Thank you for this performance work. Swapping in this version reduced the build time for a 112MB, 228 page PDF containing over 4500 images from 40 minutes to 30 seconds - an awesome improvement. |
This offers a huge performance benefit, any chance this will get merged? |
@alallier you may want to check out #171 and also check out this link to see that the fpdf2 fork uses a bytearray instead of a string (and is otherwise maintained): https://github.com/PyFPDF/fpdf2/blob/master/fpdf/fpdf.py#L178 |
Thanks for the link. I noticed shortly after that this project was abandoned which is unfortunate |
With this speedup, when you have various pages with png images, the
improvements is significant.
As a example, I used an old dual core machine, with 104 png images (16MB per image), to
generate a pdf, 1 image per page. The time consuming before was 1 minute
and 28 seconds. After this patch, the time is 6 seconds!!!
Yes, 6 seconds, 14,7x faster!