HiveBrain v1.2.0
Get Started
← Back to all entries
patternMajor

"For small values of n, O(n) can be treated as if it's O(1)"

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
cantreatedsmallforvalues

Problem

I've heard several times that for sufficiently small values of n, O(n) can be thought about/treated as if it's O(1).

Example:

The motivation for doing so is based on the incorrect idea that O(1)
is always better than O(lg n), is always better than O(n). The
asymptotic order of an operation is only relevant if under realistic
conditions the size of the problem actually becomes large. If n stays
small then every problem is O(1)!

What is sufficiently small? 10? 100? 1,000? At what point do you say "we can't treat this like a free operation anymore"? Is there a rule of thumb?

This seems like it could be domain- or case-specific, but are there any general rules of thumb about how to think about this?

Solution

This is largely piggy-backing on the answers already posted, but may offer a different perspective.

It's revealing that the question discusses "sufficiently small values of n". The whole point of Big-O is to describe how processing grows as a function of what's being processed. If the data being processed stays small, it's irrelevant to discuss the Big-O, because you're not interested in the growth (which isn't happening).

Put another way, if you're going a very short distance down the street, it may be equally fast to walk, use a bicycle, or drive. It may even be faster to walk if it would take a while to find your car keys, or if your car needs gas, etc.

For small n, use whatever's convenient.

If you're taking a cross-country trip, then you need to look at ways to optimize your driving, your gas mileage, etc.

Context

StackExchange Computer Science Q#35385, answer score: 45

Revisions (0)

No revisions yet.