|Perl Monk, Perl Meditation|
Re: A mod2 Machine.by RichardK (Vicar)
|on Jul 04, 2013 at 12:01 UTC||Need Help??|
I have to disagree with you conclusions, you're just not comparing like with like.
Which technique is faster depends entirely on the data format. If the data is in binary then you will only have to test the least significant bit, and one bit test will be faster than comparing the last digit against a list of alternatives.
OTOH converting text to binary then performing a mathematical operation may be slower than just operating on the text directly. But you will have to identify the last digit in the text and so will probable be running a regex, which are complex and therefore take time.
So exactly which approach is faster will depend on many factors and will take comprehensive testing to determine which is best.
Modern processors are highly complex and highly optimised and just assuming that mathematical operations are slow is a mistake. You really have to carefully test your code to determine which approach is better in any particular situation. And even they you've only optimised it for one platform if you want your code to portable then that's even more difficult.
In general it's much better to design a simple algorithm that's easy for a human to understand and let the interpreter/compiler & processor work out the best way to run it. Compilers and processors have lots of very clever optimization techniques and they are usually better at turning source code into running code that we are, so let them do their job.
The conclusions you should draw are