13

For example, int(x) float(x) str(x) What is time complexity of them?

kaya3
  • 47,440
  • 4
  • 68
  • 97
HIPPO LD
  • 477
  • 5
  • 20
  • It depends on the argument type. `int(x)` is O(1) if `x` is already an `int`, but it's O(n) for a `str` of length `n`. – chepner Mar 21 '20 at 14:01
  • 4
    btw, there is no casting in python. What you are using are conversion functions. – quamrana Mar 21 '20 at 14:02
  • 3
    `str` can be arbitrarily complex because it calls the object's `__str__` method which is free to do whatever it likes. – tdelaney Mar 21 '20 at 14:11

2 Answers2

10

There is no definite answer to this because it depends not just what type you're converting to, but also what type you're converting from.

Let's consider just numbers and strings. To avoid writing "log" everywhere, we'll measure the size of an int by saying n is how many bits or digits it takes to represent it. (Asymptotically it doesn't matter if you count bits or digits.) For strings, obviously we should let n be the length of the string. There is no meaningful way to measure the "input size" of a float object, since floating-point numbers all take the same amount of space.

  • Converting an int, float or str to its own type ought to take Θ(1) time because they are immutable objects, so it's not even necessary to make a copy.
  • Converting an int to a float ought to take Θ(1) time because you only need to read at most a fixed constant number of bits from the int object to find the mantissa, and the bit length to find the exponent.
  • Converting an int to a str ought to take Θ(n2) time, because you have to do Θ(n) division and remainder operations to find n digits, and each arithmetic operation takes Θ(n) time because of the size of the integers involved.
  • Converting a str to an int ought to take Θ(n2) time because you need to do Θ(n) multiplications and additions on integers of size Θ(n).
  • Converting a str to a float ought to take Θ(n) time. The algorithm only needs to read a fixed number of characters from the string to do the conversion, and floating-point arithmetic operations (or operations on bounded int values to avoid intermediate rounding errors) for each character take Θ(1) time; but the algorithm needs to look at the rest of the characters anyway in order to raise a ValueError if the format is wrong.
  • Converting a float to any type takes Θ(1) time because there are only finitely many distinct float values.

I've said "ought to" because I haven't checked the actual source code; this is based on what the conversion algorithms need to do, and the assumption that the algorithms actually used aren't asymptotically worse than they need to be.

There could be special cases to optimise the str-to-int conversion when the base is a power of 2, like int('11001010', 2) or int('AC5F', 16), since this can be done without arithmetic. If those cases are optimised then they should take Θ(n) time instead of Θ(n2). Likewise, converting an int to a str in a base which is a power of 2 (e.g. using the bin or hex functions) should take Θ(n) time.

kaya3
  • 47,440
  • 4
  • 68
  • 97
  • why conversion of str->int and str->float are not the same? – Rustam-Z Jan 31 '22 at 03:37
  • I think str->int takes O(n), you can easily do it over looping the string, then sum `sum_ += i * 10**end_index` end_index is the last element's index in str – Rustam-Z Jan 31 '22 at 03:40
  • @Rustam-Z That would also be an O(n^2) algorithm. `10**end_index` takes O(n) time because it creates an `int` object with O(n) bits, and the additions would be O(n) each for the same reason. – kaya3 Jan 31 '22 at 06:14
  • the complexity of math.pow(x, y) is O(1) -> https://stackoverflow.com/questions/48839772/why-is-time-complexity-o1-for-powx-y-while-it-is-on-for-xy – Rustam-Z Jan 31 '22 at 08:21
  • 1
    @Rustam-Z That's simply wrong. Read the answer on your link. – kaya3 Jan 31 '22 at 11:47
-2

Float(x) is more complex among these, as it has a very long range. At the same time it depends on how much of the value you are using.

Brand0R
  • 1,413
  • 11
  • 17
Advik
  • 1
  • 1
    Regarding range: `float` is the smallest type, as it has a finite number of values it can store. The number of possible `int` and `str` values is limited only by your machine's memory. – chepner Mar 21 '20 at 14:28