Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: A mod2 Machine.

by code-ninja (Scribe)
on Jul 08, 2013 at 07:43 UTC ( [id://1043049]=note: print w/replies, xml ) Need Help??


in reply to Re: A mod2 Machine.
in thread A mod2 Machine.

Ok, I'll be more clear. First, when I said that the arithmetic operation will take more time, I said it with respect to the mod2 machine. Second, let's take your example (just a wild guess, is this game GO)

#!/usr/bin/perl # note that %b modifier processes binary. Similarly, %x processes hexa +decimal and %o is for octal. use strict; use warnings; my ($move, $pos); $move = 0b1111111111111111; $pos = 0b0000111111111111; printf "%b\n", ($move ^ $pos);

Even here, there are no arithmetical operations. Note that Exclusive Or is a logical operation. This is somewhat similar to a mod machine. You change state only if there are opposite inputs. The catch here is, if we'd have used strings instead of a number, there would've been a few more lines of code, keeping in mind and as you said, breaking the string and using regexes. I'm not saying that we should replace every arithmetic operation with string/logical operation, we just can't, I'm just trying to convey that there are better ways to do simple things that school teaches us in a very dogmatic way.

I mean seriously, why should I actually divide a number by 2 and check the remainder when I can directly check the last digit?! Or why should I recursively divide and mod by 10 to generate the reverse of a number when I can do it in one line?!

/* Apologies for using C, but its my "mother-tongue" :P */ #include <stdio.h> int main(int argc, char **argv) { int ; for(i = (strlen(argv[1]) - 1); i >= 0; i--){ printf("%c", *(argv[1] + i)); /* reverse a string, a number, a + sentence, a novel... */ } }
Note/PS: Logical operations, too, are faster than arithmetical operations afaik.

Replies are listed 'Best First'.
Re^3: A mod2 Machine.
by Laurent_R (Canon) on Jul 08, 2013 at 11:54 UTC

    I think we agree.

    My point is that I was using the ^ logical operation on actual binaries, something very similar to what you showed:

    my ($move, $pos); $move = 0b1111111111111111; $pos = 0b0000111111111111; printf "%b\n", ($move ^ $pos);

    But I needed to show that using actual binaries, such as 0b0000111111111111 was far superior to using binary strings, i.e. something like "0000111111111111". To do this, I needed to find the best possible way to do the equivalent of ^ for strings. And it turned out that the fastest way I found was to add the strings (meaning an implicit conversion of the string to a digit), giving a result like 1111222222222222, and then (with another implicit conversion) replacing the twos by zeros (with a command like tr/2/0/) to finally get "1111000000000000". And that was four times slower than the logical ^ on actual binaries. But it was still the fastest way to do it on strings. So that, in that case, arithmetics was faster than, for example, regexes or splitting the strings into individual characters to process them one by one..

    I mean seriously, why should I actually divide a number by 2 and check the remainder when I can directly check the last digit?!

    I definitely agree with you on that. I was only saying that there are some other cases where arithmetics is faster than other means. Although, in my case, the best, by far, was to use actual binary nombers and a logical exclusive or (4 fimes faster than artithmetics on binary strings).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1043049]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-03-28 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found