StringStream を使用して巨大文字列を構築する

文字列を構築する場合は、StringStream 値を使用して文字列を累積してから文字列に変換します。

文字列を追加するたびに新しい文字列が作成されます。

例:

    a ="AAA"
    b = a + a + a +a + a + a
    --Creates 6 strings of length 3, 6, 9, 12, 15, 18
    a ="AAA"
    b = (a + a + a) + (a + a + a)
    --Creates 6 strings of length 3, 6, 9, 6, 9, 18

テスト ケース:

    fn test5a =
    (
     local a =""
     for i = 1 to 100 do a += (i as string)
    )
    fn test5b =
    (
     local ss = stringstream""
     for i = 1 to 100 do format "%"(i as string) to:ss
     ss as string
    )
    fn test5c =
    (
     local ss = stringstream"",fmt ="%"
     for i= 1 to 100 do format fmt (i as string) to:ss
     ss as string
    )
    fn test5d =
    (
     local ss = stringstream"",fmt ="%"
     for i = 1 to 100 do format fmt ito:ss
     ss as string
    )

100,000 回の繰り返しの場合

    test5a()     -- 58875 msec., 505 MB
    test5b()     -- 54672 msec., 39.2 MB
    test5c()     -- 41125 msec., 29.2 MB
    test5d()     -- 13532 msec., 10.0 MB

前のヒント

return、break、exit、continue を使用しない

次のヒント

可能な場合は文字列の代わりに名前の値を使用する