[Python] very slow "<name> in font" behavior.

View: New views
7 Messages — Rating Filter:   Alert me  

[Python] very slow "<name> in font" behavior.

by JustFillBug :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


While coding a script, add a line to a loop like:

   if "glyphname" in font:

make the script slow down quite a bit.

After done a test, it seemed a replacement of

   try:
       f = font['glyphname']
   except TypeError:
       pass

eliminate the lagging. A test script is attached. Run it on a large
font you will see it.

Maybe "<name> in f" should be speed up because people will naturally
choose this method to test existing glyphnames.

BTW: Why is the exception of the type TypeError, instead of KeyError?

#!/usr/bin/python
# vim:ts=8:sw=4:expandtab:encoding=utf-8

import sys
import fontforge
import time

def main():
    if len(sys.argv) != 2:
        print 'Usage:\n\t%s fontfile.ttf' % sys.argv[0]
        return -2
    font = fontforge.open(sys.argv[1])

    print 'test 1',
    sys.stdout.flush()
    t1 = time.clock()

    # <name> in f
    for i in range(1000):
        if 'test' in font:
            pass

    t2 = time.clock()
    print t2 - t1

    print 'test 2',
    sys.stdout.flush()
    t1 = time.clock()

    # font[<name>]
    for i in range(1000):
        try:
            me = font['test']
        except TypeError:
            pass

    t2 = time.clock()
    print t2 - t1

if __name__ == '__main__':
    main()



------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Fontforge-users mailing list
Fontforge-users@...
https://lists.sourceforge.net/lists/listinfo/fontforge-users

Re: [Python] very slow "<name> in font" behavior.

by Barry Schwartz-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

JustFillBug <mozbugbox@...> skribis:

> While coding a script, add a line to a loop like:
>
>    if "glyphname" in font:
>
> make the script slow down quite a bit.
>
> After done a test, it seemed a replacement of
>
>    try:
>        f = font['glyphname']
>    except TypeError:
>        pass
>
> eliminate the lagging. A test script is attached. Run it on a large
> font you will see it.

That would be the expected behavior for a brute force implementation,
because in that case the "in" operator implies a loop.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Fontforge-users mailing list
Fontforge-users@...
https://lists.sourceforge.net/lists/listinfo/fontforge-users

Re: [Python] very slow "<name> in font" behavior.

by Max Rabkin-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Sep 7, 2009 at 7:10 AM, Barry
Schwartz<chemoelectric@...> wrote:
> That would be the expected behavior for a brute force implementation,
> because in that case the "in" operator implies a loop.

It only implies a loop for certain data structures, and the font
clearly *isn't* such a structure, since font['glyphname'] is fast.

--Max

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Fontforge-users mailing list
Fontforge-users@...
https://lists.sourceforge.net/lists/listinfo/fontforge-users

Re: [Python] very slow "<name> in font" behavior.

by Barry Schwartz-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Max Rabkin <max.rabkin@...> skribis:
> On Mon, Sep 7, 2009 at 7:10 AM, Barry
> Schwartz<chemoelectric@...> wrote:
> > That would be the expected behavior for a brute force implementation,
> > because in that case the "in" operator implies a loop.
>
> It only implies a loop for certain data structures, and the font
> clearly *isn't* such a structure, since font['glyphname'] is fast.

Oh, you are right. D'oh!


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Fontforge-users mailing list
Fontforge-users@...
https://lists.sourceforge.net/lists/listinfo/fontforge-users

Re: [Python] very slow "<name> in font" behavior.

by George Williams :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, 2009-09-06 at 21:44, JustFillBug wrote:
> While coding a script, add a line to a loop like:
>
>    if "glyphname" in font:
>
> make the script slow down quite a bit.
Unfortunately I do not know the guts of python well enough to know for
sure how to speed this up. Nor can I find the appropriate info in the
docs.

The cvs tree contains a guess which might improve things, but I make no
promises.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Fontforge-users mailing list
Fontforge-users@...
https://lists.sourceforge.net/lists/listinfo/fontforge-users

Re: [Python] very slow "<name> in font" behavior.

by Tim Doty-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sunday 06 September 2009, JustFillBug wrote:
> While coding a script, add a line to a loop like:

I ran your script on Cyberbit.ttf, a 13MB font. After bumping the number of
iterations in the loops to a million I got a measurable time delay.

test 1 0.19
test 2 3.68

which indicates the faster method is the first test.

I got similar results on a different (more normal sized) font I tried.

Tim Doty

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Fontforge-users mailing list
Fontforge-users@...
https://lists.sourceforge.net/lists/listinfo/fontforge-users

Re: [Python] very slow "<name> in font" behavior.

by JustFillBug :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 2009-09-12, Tim Doty <thoromyr@...> wrote:

> On Sunday 06 September 2009, JustFillBug wrote:
>> While coding a script, add a line to a loop like:
>
> I ran your script on Cyberbit.ttf, a 13MB font. After bumping the number of
> iterations in the loops to a million I got a measurable time delay.
>
> test 1 0.19
> test 2 3.68
>
> which indicates the faster method is the first test.
>
> I got similar results on a different (more normal sized) font I tried.
>

Are you using the current CVS? George just fixed the slowness in the cvs
a few days ago.

And we can also use "<encoding> in font", now!

Since the test 2 uses execption in case of failure, it will suffer a
bigger penalty whenever an exception is thrown.

If the test is positive, only a small extra cost of glyph object
creation will show up for test 2.



------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Fontforge-users mailing list
Fontforge-users@...
https://lists.sourceforge.net/lists/listinfo/fontforge-users