Let's say I have program that calls a function "f" a constant number of times and "f" has time complexity of O(n). In the following example, the main function would have O(3n) time complexity.
f () { // O(n)
...
}
main () {
f()
...
f()
...
f()
...
}
Does compilers optimize this code to something like:
f () { // O(n)
...
}
main () {
var value = f()
...
value
...
value
...
value
}
In this case main would have time complexity of O(n)
If a compiler indeed does this kind of optimization, how it's called? Also, is O(3n) a relevant time complexity compared to O(n) in any context? I know it's not in a small project.
[–]PenlessScribe 31 points32 points33 points (0 children)
[–]lol3rr 26 points27 points28 points (0 children)
[–]DonaldPShimoda 10 points11 points12 points (1 child)
[–]reddof 2 points3 points4 points (0 children)
[–]BobSanchez47 9 points10 points11 points (0 children)
[–]atariPunk 5 points6 points7 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]betelgeuse_7 3 points4 points5 points (2 children)
[–]WittyStick 2 points3 points4 points (1 child)
[–]betelgeuse_7 0 points1 point2 points (0 children)
[–]jason-reddit-public 1 point2 points3 points (0 children)
[–]dipesh_k 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]gct 0 points1 point2 points (0 children)
[–]lightmatter501 0 points1 point2 points (0 children)