Asymptotic complexity printf

Assuming I'm printing a line as follows:

printf("%s", s); 

What can we consider the asymptotic complexity of this function?

Is it O (n), where n is strlen (s) is the length? Or is it somehow O (1), constant time. Or something different? I suggested that you need to know how printf is typically implemented. Any insight appreciated!

(I have to clarify what I'm talking about C, not C ++, but I doubt that they are implemented differently)

Edit: added format string in printf ()

+6
source share
1 answer

Complexity is O (m + n), where m is the size of the input and n is the size of the output file.

If you do not pass additional parameters, for example, in your case, the time complexity is O (2 * m) = O (m).

But keep in mind that your code may fail because s may contain the formatting codes themselves, and this will result in the result undefined / unknown / unpredictable / possible_very_bad, as indicated by Adriano.

+7
source

Source: https://habr.com/ru/post/946095/


All Articles