2

関数の属性@safepureおよびnothrow親関数のコンパイル時と実行時のパフォーマンスに関して、文字列を構築するための推奨される方法は何ですか?

たとえば、どちらを使用する必要がありますか

format("Variable x=%s should equal %s", x, x_)

また

"Variable x=" ~to!string(x)~ " should equal " ~to!string(x_)

formatバージョンの方が見やすいと思いますが、他の点では優れていますか?

フォーマット文字列とその引数を CTFE する必要があるため、コンパイル時にわずかなパフォーマンス ヒットが発生する可能性があります。

4

2 に答える 2

0

受け入れられた答えが言うように、format!を好み、テンプレート引数としてフォーマット文字列を渡すことを好みます。

#!/usr/bin/env rdmd

module test;

import std.format; // or std.string, which publicly imports std.format
import std.stdio;

void main()
{
    writeln(format("%s%s","run-time error 'Orphan format specifier'"));
    writeln(format!"%s%s"("compile-time error 'Orphan format specifier'"));
}

2017 年 4 月にリリースされたDMD 2.074.0以降で使用できます。

于 2018-08-23T09:16:23.900 に答える