Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

performance improvements #60

wants to merge 3 commits into from

Conversation

marcelotduarte
Copy link
Contributor

@marcelotduarte marcelotduarte commented Feb 16, 2016

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!

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"
Copy link
Collaborator

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

Copy link
Contributor Author

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?

Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right!

@vadmium
Copy link
Collaborator

vadmium commented Feb 16, 2016

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.

@marcelotduarte
Copy link
Contributor Author

@reingart: This patch is good to be merged?

@aussig
Copy link

aussig commented Jan 10, 2019

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.

@alallier
Copy link

This offers a huge performance benefit, any chance this will get merged?

@alexp1917
Copy link

@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

@alallier
Copy link

Thanks for the link. I noticed shortly after that this project was abandoned which is unfortunate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants