C-String vs Number Comparison

A quick question, say if a number is derived and stored in C-String, and I would like to do a comparison. Would it be more efficient memory and CPU wise to compare it directly as a string, or parse it to a number first? For instance:

char myString[] = "12345678";

if( strcmp_P(myString, PSTR("12345678")) == 0 ) //Is this better?
{
  //DO Something
}

if( atol(myString) == 12345678 ) //Or is this better?
{
  //DO Something
}

Would it be more efficient memory and CPU wise to compare it directly as a string, or parse it to a number first?

Converting to a number is far more compute-intensive.

Thanks Paul, may I ask would either one consume more memory than the other in the comparison process, or would it not make a difference?

PaulS:
Converting to a number is far more compute-intensive.

I suspect strcmp_P (scan two strings, read from Flash, with comparison) is more expensive than atol (scan one string with convert) followed by an integer comparison.

I was very wrong. 6.351 μs for strcmp_P; 22.008 μs for atol.

atol takes 94 bytes more Flash. Neither uses SRAM.

With enough string constants strcmp_P will eventually consume more Flash.

Coding Badly thanks, exactly the answer I was looking for. :slight_smile:

You are welcome.

Thank you to @PaulS for keeping me on the right path.

Which is greater, "987" or "12345" ?

Well, "9" is greater than "1" so strcmp would indicate "987" is greater. The integer 987 is less than 12345 so atol would indicate 12345 is greater.