Search code examples
pythonperformancematplotlibadobe

Extremely slow import of matplotlib afm


Import time is unacceptably long:

  from matplotlib import pylab as plt --> 3.0124739 secs

This is the output of cProfile, which clearly shows the problem being afm.py, which is some interface for Adobe Font Metrics.. Matplotlib version is 1.4.0 - Ubuntu 14.04

         5994612 function calls (5992123 primitive calls) in 4.702 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      182    1.589    0.009    2.372    0.013 afm.py:220(_parse_kern_pairs)
      215    0.746    0.003    1.420    0.007 afm.py:180(_parse_char_metrics)
  1297990    0.410    0.000    0.410    0.000 {method 'split' of 'str' objects}
   839978    0.288    0.000    0.288    0.000 {method 'readline' of 'file' objects}
        2    0.254    0.127    4.185    2.093 font_manager.py:544(createFontList)
   843308    0.170    0.000    0.170    0.000 {method 'startswith' of 'str' objects}
   114276    0.168    0.000    0.205    0.000 afm.py:72(_to_list_of_floats)
1563800      0.125    0.000    0.125    0.000 {len}
   839220    0.117    0.000    0.117    0.000 {method 'rstrip' of 'str' objects}
   115290    0.116    0.000    0.119    0.000 {method 'decode' of 'str' objects}
   117775    0.086    0.000    0.086    0.000 afm.py:56(_to_int)
       20    0.061    0.003    0.061    0.003 {built-in method poll}
      520    0.040    0.000    0.040    0.000 {method 'get_sfnt' of 'FT2Font' objects}
     6910    0.036    0.000    0.036    0.000 {posix.stat}
     3372    0.025    0.000    0.038    0.000 posixpath.py:336(normpath)

Any idea how to fix/disable it?

EDIT:

Rolling back to 1.3.1 saved my day. Running time is now 0.2 secs.


Solution

  • I had the same problem. I figured out, that this happens when you upgrade matplotlib to a newer version. The font cache file is not compatible with the new version, but apparently is not recreated. Instead font list is created each time on the fly. The solution is to delete the matplotlib cache directory (in my case ~/.cache/matplotlib)