There is a module call
Net:Patricia which allows you to perform such IP class matches very quickly using the Patricia Trie structure. The methods provided by this module are very friendly and very easy to use. An example of using this module can be seen in my post
IP restricted mail delivery.
Ooohhh, Rob no beer function well without!