Halhelms
SIGN UP FOR MY NEWSLETTER

www.savorgold.com is top on wow gold and runescape gold and World of Warcraft gold provider list for trusted services. Their reputation seems to growing by the minute, which isn't surprising because they are one of the safest sellers of Gold. Delivery speed and customer service are very good. They aslo are giving some bonus items depending on the amount of gold you purchase.

 
 
Halhelms

Recent Comments

Recent Entries

RSS

A Little Code Puzzle : II

Here's another little puzzle. Here, we're trying to come up with the least processor-intensive algorithm for determining, given an array of random integers, how many are even?

I have a solution that works, but I'm not sure it's the most efficient. What's your solution? (If you want to see mine, highlight the space below -- the code is white on white).

function evens( arr ) {
	var numFound = 0;
	var i = arr.length;
	while ( i-- ){
		if ( arr[ i ] % 2 > 0 ) {
			continue;
		} else{
			numFound++;
		}
	}
	alert ( numFound );
}

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Joseph Zack's Gravatar I've always heard that using bitwise "and" was a faster way to figure out if a number was odd.

var isOdd = someNumber & 1;
# Posted By Joseph Zack | 11/20/10 6:22 PM
Hal Helms's Gravatar Nice, Joseph.
# Posted By Hal Helms | 11/20/10 6:24 PM
Jon Dowdle's Gravatar If you combine the filter method (available in most JavaScript implementations) with Joseph's point you can do something like this in one line.

evens = [22,33,2,11,102,5].filter( function(i,n){ return !(i & 1);})
# Posted By Jon Dowdle | 11/20/10 7:34 PM
Ron Stewart's Gravatar I have a hunch that the least processor intensive algorithm may well depend upon the language you implement the solution in. For small arrays, it's probably going to be tough to measure an appreciable difference between your solution and Jon's, and it may or may not be an intuitive answer as to which of those performs better on large datasets. And it's possible the answer will differ enough for very small or very large datasets, that I'd want to know more about the most-likely usage before I'd settle on one, and if there is a significant performance difference between the performance of the algorithms based on dataset size, I might consider an adaptive approach...

How's that for a non-answer?

I chose that answer because I just bumped into something like this last week in writing an editor macro to apply some basic formatting to an XML file. After I got the basics working, I started looking at how to improve the performance on large files and several tweaks that seemed likely to improve performance actually had the opposite effect.
# Posted By Ron Stewart | 11/21/10 12:11 PM
Joe D'Angelo's Gravatar Why the else clause in your conditional? I'd leave it at
if (arr[i]%2==0){numFound++}
# Posted By Joe D'Angelo | 11/22/10 1:52 PM
Hal Helms's Gravatar Good point, Joe.

VERY good point, Ron.
# Posted By Hal Helms | 11/22/10 1:59 PM
Bob Walasek's Gravatar You could actually get rid of the conditional completely and incorporate Joseph's comment to do:

function evens(arr){
var numFound = 0;
var i = arr.length;
while (i--) {
numFound += (arr[i]+1) & 1; // increment the number being tested so that an even generates an incrementer
}
return numFound;
}
# Posted By Bob Walasek | 11/22/10 5:56 PM
Matt Osbun's Gravatar Doing a little necromancy on a dead thread, but I'm running some logging tests at work, which is about as interesting as watching grass grow, so I started going through your blog. :)

AFAIK, you can use a Bitwise AND to MOD powers of two. I'm pretty sure, though, that C-based compilers use MOD(x,y) = x-y*Floor(x/y). Might want to try that for speed.

For the record, in C# I'd use:

new int[]{1,2,3,4,5,6,7,8,9,10}.Where(arg => arg%2 == 0).Count()
# Posted By Matt Osbun | 4/14/11 4:08 PM
 
   
Clicky Web Analytics