Skip to content

Instantly share code, notes, and snippets.

@WoozyMasta
Created December 25, 2025 07:09
Show Gist options
  • Select an option

  • Save WoozyMasta/df8f8388b0a38fac707d4c1af96a3bad to your computer and use it in GitHub Desktop.

Select an option

Save WoozyMasta/df8f8388b0a38fac707d4c1af96a3bad to your computer and use it in GitHub Desktop.
EnforceScript: String Concatenation vs string.Format

EnforceScript: String Concatenation (+) vs string.Format()

If you're still using string concatenation, just don't. It's absolute trash, even if you're just joining two simple strings.
Always use string.Format() whenever possible!

Check out the benchmark results below. I totally expected the gap on 9 parameters, but the difference on just 2 parameters? That actually shocked me.

SCRIPT       : ### BENCHMARK STARTED: String Concatenation vs string.Format ###
SCRIPT       : >> Running Round 1 of 10...
SCRIPT       : >> Running Round 2 of 10...
SCRIPT       : >> Running Round 3 of 10...
SCRIPT       : >> Running Round 4 of 10...
SCRIPT       : >> Running Round 5 of 10...
SCRIPT       : >> Running Round 6 of 10...
SCRIPT       : >> Running Round 7 of 10...
SCRIPT       : >> Running Round 8 of 10...
SCRIPT       : >> Running Round 9 of 10...
SCRIPT       : >> Running Round 10 of 10...
SCRIPT       : ### BENCHMARK FINISHED ###
SCRIPT       : Cycles per round: 100000 | Rounds: 10
SCRIPT       : --------------------------------------------------
SCRIPT       : 2 Params | PLUS (+)      : Avg Time: 0.0303997 s
SCRIPT       : 2 Params | string.Format : Avg Time: 0.0189007 s
SCRIPT       : --------------------------------------------------
SCRIPT       : 9 Params | PLUS (+)      : Avg Time: 0.117199 s
SCRIPT       : 9 Params | string.Format : Avg Time: 0.0542004 s
SCRIPT       : --------------------------------------------------
SCRIPT       : Winner (2 params): FORMAT
SCRIPT       : Winner (9 params): FORMAT
SCRIPT       : ##################################################
modded class MissionServer : MissionBase
{
override void OnInit()
{
super.OnInit();
GetGame().GetCallQueue(CALL_CATEGORY_SYSTEM).CallLater(RunStringsTest, 15 * 1000, false);
}
void RunStringsTest()
{
Print("### BENCHMARK STARTED: String Concatenation vs string.Format ###");
int i, k;
const int CYCLES = 100000; // Number of operations in one run
const int ROUNDS = 10; // Number of runs for averaging
// test 2 params
string a = "Hello";
string b = "World";
// test 9 params
string p1 = "One";
string p2 = "Two";
string p3 = "Three";
string p4 = "Four";
string p5 = "Five";
string p6 = "Six";
string p7 = "Seven";
string p8 = "Eight";
string p9 = "Nine";
string output;
float time_2_concat_total = 0;
float time_2_format_total = 0;
float time_9_concat_total = 0;
float time_9_format_total = 0;
float t_start, t_end, t_diff;
for (k = 0; k < ROUNDS; k++) {
Print(string.Format(">> Running Round %1 of %2...", k + 1, ROUNDS));
t_start = GetGame().GetTickTime();
for (i = 0; i < CYCLES; i++)
output = "string a: [" + a + "] & string b: [" + b + "]";
t_end = GetGame().GetTickTime();
time_2_concat_total += (t_end - t_start);
t_start = GetGame().GetTickTime();
for (i = 0; i < CYCLES; i++)
string.Format("string a: [%1] & string b: [%2]", a, b);
t_end = GetGame().GetTickTime();
time_2_format_total += (t_end - t_start);
t_start = GetGame().GetTickTime();
for (i = 0; i < CYCLES; i++)
output = p1 + " - " + p2 + " - " + p3 + " - " + p4 + " - " + p5 + " - " + p6 + " - " + p7 + " - " + p8 + " - " + p9;
t_end = GetGame().GetTickTime();
time_9_concat_total += (t_end - t_start);
t_start = GetGame().GetTickTime();
for (i = 0; i < CYCLES; i++)
string.Format("%1 - %2 - %3 - %4 - %5 - %6 - %7 - %8 - %9", p1, p2, p3, p4, p5, p6, p7, p8, p9);
t_end = GetGame().GetTickTime();
time_9_format_total += (t_end - t_start);
}
// results
Print("### BENCHMARK FINISHED ###");
Print(string.Format("Cycles per round: %1 | Rounds: %2", CYCLES, ROUNDS));
Print("--------------------------------------------------");
// avg
float avg_2_concat = time_2_concat_total / ROUNDS;
float avg_2_format = time_2_format_total / ROUNDS;
float avg_9_concat = time_9_concat_total / ROUNDS;
float avg_9_format = time_9_format_total / ROUNDS;
Print(string.Format("2 Params | PLUS (+) : Avg Time: %1 s", avg_2_concat));
Print(string.Format("2 Params | string.Format : Avg Time: %1 s", avg_2_format));
Print("--------------------------------------------------");
Print(string.Format("9 Params | PLUS (+) : Avg Time: %1 s", avg_9_concat));
Print(string.Format("9 Params | string.Format : Avg Time: %1 s", avg_9_format));
// total
string winner2 = "PLUS";
if (avg_2_format < avg_2_concat)
winner2 = "FORMAT";
string winner9 = "PLUS";
if (avg_9_format < avg_9_concat)
winner9 = "FORMAT";
Print("--------------------------------------------------");
Print(string.Format("Winner (2 params): %1", winner2));
Print(string.Format("Winner (9 params): %1", winner9));
Print("##################################################");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment