Omnimaga
Calculator Community => TI Calculators => TI-BASIC => Topic started by: blue_bear_94 on October 28, 2013, 05:29:58 pm
-
In your opinion, should string comparisons be treated as O(1) (constant-time) or O(n) (linear time)? Technically testing for equality should consist of comparing each character, so it should be linear-time, but doing so is fast compared to interpreting the "=" operator.
-
That depends on your implementation of string comparison. The complexity is based on actual runtime.
If you have some fancy hardware hasher or comparison generator, then it would be O(1), but it's O(n) most of the time.
-
I meant with the default comparison operator.
-
That implementation varies between compilers and between architectures.
Oh wait, TI-BASIC sub
-
Hmm... in the worst case, comparing both 1-character strings and 999-character strings takes less than 20 ms.
-
Why are we talking about complexity in TI-BASIC? This is a case where actual runtime speed makes far more difference than complexity. I would take an n^2 algo that is twice as fast for low n over an n log n.
-
I'm writing a TI-Basic book.
-
Technically testing for equality should consist of comparing each character, so it should be linear-time, but doing so is fast compared to interpreting the "=" operator.
Time complexity has nothing to do with how "fast" or "slow" a particular implementation is. It's how quickly a problem scales with the size of the input. Since strings have to be read character by character and the length of a string in characters is O(n), then the time complexity to compare two strings is equivalent to reading two strings, assuming character comparison is O(1). Thus, we have strcmp() = O(n)+O(n) = O(n)
-
^ this for TI-BASIC in particular. No way does the z80 have any fancy string comparison stuff in its hardware.