Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one is faster and why?

(n >= 3 ) && (n <= 99)

OR

 n `elem` [3..99]

Which one is faster and why?

like image 978
Pratik Deoghare Avatar asked Mar 11 '10 09:03

Pratik Deoghare


People also ask

Which is faster light or sound and why?

The speed of light as it travels through air and space is much faster than that of sound; it travels at 300 million meters per second or 273,400 miles per hour. Visible light can also travel through other things besides through air and through space.

Which one is faster light or electricity?

Light travels through empty space at 186,000 miles per second. The electricity which flows through the wires in your homes and appliances travels much slower: only about 1/100 th the speed of light.

Which have more speed light or sound?

No, sound travels much slower than the speed of light. The speed of sound in air is about 340 metres per second. Whereas, the light will travel through the vacuum at a speed of 300 million metres per second. The speed of light and sound varies in different mediums.

What travels faster in water light or sound?

Light travels faster than sound even in water. If you are asking why sound is slower when it is in air than water, and why light is faster in air than in water, here is why: Light waves are electromagnetic transversal waves.


1 Answers

The first one is faster

 (n >= 3) && (n <= 99)

it is doing 3 operations

 n >= 3
 n <= 99
 and

Where as the elem is looking up the item in the array, so is doing upto (99 - 3) * 2 operations.

index = 0
isFound = false
array[] = { 3, 4, 5, 6, ... 98, 99 }

while isFound == false
   isFound = (n == array[index++])
like image 57
Dead account Avatar answered Nov 01 '22 16:11

Dead account