Assuming in your hypothetical language !=
means "compare" and =
means "copy"...
I'm going to say that UpdateString1
is always at least as fast.
Suppose the strings aren't equal. Then UpdateString2
performs the comparison as well as the assignment. So it does additional work.
Suppose the strings are equal. Then the comparison involves iterating through every single character in both strings and comparing them. So that's O(n). Similarly, copying would involve, at worst, visiting every character in one string and copying it to the second. Also O(n). So the same complexity. Also the same number of memory accesses.
However you've also got the partial comparison costs of the strings that aren't equal. Which I think tips it in favour of the copy.
Supposing !=
and =
were just comparing or updating references by identity, not by value...
All operations are O(1) and simuilar in cost. The =
is one operation 100% of the time. The !=
/=
is an expected 1.5 operations if strings are equal 50% of the time.