jai-like CTFE string formating

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Sun Aug 13 11:25:34 PDT 2017


On Sunday, 13 August 2017 at 18:20:15 UTC, Sönke Ludwig wrote:
> Am 12.08.2017 um 13:47 schrieb Stefan Koch:
>> Hi Guys,
>> 
>> I've just implemented a subset of the std.format functionality.
>> In the same style that Johnathan Blow uses for JAI.
>> 
>> It's about 10x faster then using std.format and uses much less 
>> memory :)
>> 
>> the follwing code takes over 250 ms to compile :
>> {
>>      import std.format;
>>      pragma(msg, format("Hello %s %s %s %s %s %s %s %s %s", " 
>> I ",
>>   "just", " have", " to", " concat",  " a lot", " of", " 
>> strings ...", 9));
>> }
>> Whereas the following alternative takes 20 ms :
>> {
>>      import ctfe_utils;
>>      pragma(msg, format_jai("Hello % % % % % % % % %", " I ", 
>> " just", " have" , " to", " concat", " a lot", " of", " 
>> strings ...", 9));
>> }
>> 
>> see for yourself:
>> https://www.youtube.com/watch?v=T0BJxdt61RY
>
> I was a bit shocked by this number and performed a little test 
> with multiple calls to format. Fortunately only the first one 
> takes that long to compile. For 500 different calls I got about 
> 2ms per call on Windows (which might get slowed down somewhat 
> by the slow terminal output):
>
>     import std.format;
>     template T(size_t i) {
>       static if (i > 0) {
>         pragma(msg, format("Bye %s %s %s %s %s %s %s %s %s %s",
>           i, " I ",  "just", " have", " to", " concat",  " a 
> lot",
>           " of", " strings ...", 9));
>         enum T = T!(i-1);
>       } else enum T = 0;
>     }
>     enum test = T!500;

Wouldn't you say that 2ms is still a pretty long time for a 
function call ?



More information about the Digitalmars-d mailing list