Group Abstract Group Abstract

Message Boards Message Boards

1
|
3.6K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:
GROUPS:

StringTemplate performance

For years I've been using constructs like:

ToString[#] <> ".png" & /@ Range[1000]

To create a bunch of file names that I use later for exporting. I was very excited about the new StringTemplate functionality. Replacing my above code to:

StringTemplate["`1`.png"] /@ Range[1000]

Which is much neater in my opinion. The problem however is the (lack of) performance:

a = First@RepeatedTiming[ToString[#] <> ".png" & /@ Range[1000];] ;
b = First@RepeatedTiming[StringTemplate["`1`.png"] /@ Range[1000];] ;
N[b/a]

a small comparison in timing reveals that StringTemplate is roughly 130 times slower than my previous approach!! Of course 0.3 seconds is OK for a 1000 filenames. But sometimes I have 10-thousands of filenames!

Is there a way to improve the performance of StringTemplate?

POSTED BY: Sander Huisman
2 Replies

Dear Udo,

The TemplateApply is equally if not slower than the StringTemplate unfortunately, thanks for the suggestion though...

--SH

POSTED BY: Sander Huisman

Formerly you got a String , now you get a TemplateObject. That matters. Have you tried FileTemplate, which constructs also a TemplateObject?

POSTED BY: Udo Krause
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard