wch / extrafont

Tools for using fonts in R graphics
312 stars 49 forks source link

Easy speed-up possible? #100

Open sergiocorreia opened 1 year ago

sergiocorreia commented 1 year ago

Hi Winston,

When running font_import() I get the message "Importing fonts may take a few minutes" and it indeed takes quite a while even if all fonts are already registered.

It seems this is because we are essentially running an O^2 task as every time we try to add a new font we re-run fonttable(), which reloads the underlying CSV file entirely.

Would it be easier to do something like fonts <- fonttable() right before we start the loop on line 78?

Thanks!

PS: Sorry for the lack of PRs; I'm quite new using R, so not entirely sure what I'm doing in that sense

wch commented 1 year ago

@sergiocorreia I haven't looked at this code in a long time, but I think you may be right that it doesn't have to read the table each time.

That said, I suspect that the time to read the CSV file is minuscule compared to the other stuff that happens in that loop. For example, if you run this, I think it should be quite fast (I don't have extrafont set up on my current computer so I haven't tested myself):

library(extrafont)
system.time({
  for (i in 1:100) {
    fonttable()
  } 
})