Reason #642 to use Ruby instead of C++ -Carpal Tunnel Syndrome

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Reason #642 to use Ruby instead of C++ -Carpal Tunnel Syndrome


    Seems like things are a bit slow on the list these days, so here's a
    little rant...

    I'm taking a class on layout algorithms (layout of wires on a chip). Most
    of these tend to be graph algorithms. One of the assignments is to
    implement the Kernighan-Lin partitioning algorithm; and it must be
    implemented in C/C++. Now there's really no good reason why it has to be
    in C++ other than maybe that's all the instructor knows how to read. So of
    course I prototyped it in Ruby first - took about 2 hours to
    implement in Ruby including debugging the algorithm and testing. Now I
    kind of thought, "Hey, I finished this thing three weeks early in
    Ruby why not ask the prof if he'd be willing to accept it in Ruby since
    it's so early?". Of course he said it _wasn't_ alright and I'd be ding'ed
    one letter grade if it's not in C or C++.

    So I dug out my Stroustoup and another book on STL. Took me about three
    hours just to get the file parsing part of it working (a simple CSV
    format). And I've been working on the algorithm part for the last day and
    a half; it's almost done, but it probably needs at least 2 or 3 more hours
    of work.

    Of course the file parsing part in Ruby was just:

    adj_hash = {}
    tmp_hash = {}
    IO.foreach(filename) {|line|
    node,*adj_nodes = line.split(',').map{|n|
    unless tmp_hash.has_key? n.strip
    tmp_hash[n.strip] = Graph::Graph::Node.new(n.strip)
    else
    tmp_hash[n.strip]
    end
    }

    That's just 10 lines. I won't post the C++ abomination for accomplishing
    the same thing here, let's just say that it's 45 lines long.

    Of course the algorithm has to do a lot of iteration over containers
    (sets), stuff like (in Ruby):

    b.each {|n|
    if adj_hsh[node].include? n
    external_cost += 1
    end
    }

    Five lines in Ruby. In C++:

    for(LI n=b.begin();n!=b.end();++n)
    {
    if(_adj_map[node].find(*n) != _adj_map[node].end())
    {
    external_cost++;
    }
    }

    Seven lines in C++ (8 if you include the typedef for the set iterator,
    LI). OK, that's not that many more lines, but there
    certainly are a lot more characters to type and it's nowhere near as
    comprehensible.

    Oh, my aching wrists!

    ....rant over.

    Phil
    Phil Tomson Guest

  2. Similar Questions and Discussions

    1. ssh 2 tunnel to named pipe not working - please help
      OK, this is officially escalated to an annoyance. I have previously been able to set up a scenario as follows: ssh user@targethost 'cat >...
    2. SSH Easy Tunnel
      Hello, We have just release a new software to manage ssh Tunnel. SSH tunnels are very easy to set up to allow external worker to get encrypt...
    3. terminal sessions over vpn tunnel getting dropped
      Using Icetcp over tunnel between Cisco PIX and VPN concentrator in Openserver 5.0.6. Some, but not all, sessions are getting dropped. I have not...
    4. Controll/ Check VPN Tunnel?
      Hi, i need to setup an VPN LAN to LAN connection between two Debian 3.0 Server. On server with static ip and one with dynamic ip. I think i have...
    5. tunnel remote desktop ...
      I've been tunneling remote desktop client thru my SSH connection (via Putty and port forwarding) from my Win2K laptop for a long time w/zero...
  3. #2

    Default Re: Reason #642 to use Ruby instead of C++ -Carpal Tunnel Syndrome

    > That's just 10 lines. I won't post the C++ abomination for accomplishing
    > the same thing here, let's just say that it's 45 lines long.
    In a similar vein, check out:

    [url]http://ai-app-prog.rubyforge.org/[/url]

    where I'm translating AI code examples from C to Ruby. As you say, lots of
    C code just goes away and gets replaced with one liners involved
    Array.collect and the like. Pretty cool!

    Yours,

    Tom


    Tom Copeland Guest

  4. #3

    Default Re: Reason #642 to use Ruby instead of C++ -Carpal Tunnel Syndrome

    Phil wrote:
    > it's so early?". Of course he said it _wasn't_ alright and I'd be ding'ed
    > one letter grade if it's not in C or C++.
    i would have taken the ding and saved the hours. life's too short! oh, maybe had a beer too. :)

    -t0

    one of these days we'll crawl out of this c hole, like fish from the sea, and monkeys from the trees. And we'll finally be free, and we'll look back and think: what c-monkeys!

    T. Onoma Guest

  5. #4

    Default Re: Reason #642 to use Ruby instead of C++ -Carpal Tunnel Syndrome

    > Of course the file parsing part in Ruby was just:
    >
    > adj_hash = {}
    > tmp_hash = {}
    > IO.foreach(filename) {|line|
    > node,*adj_nodes = line.split(',').map{|n|
    > unless tmp_hash.has_key? n.strip
    > tmp_hash[n.strip] = Graph::Graph::Node.new(n.strip)
    > else
    > tmp_hash[n.strip]
    > end
    > }
    >
    > That's just 10 lines. I won't post the C++ abomination for accomplishing
    > the same thing here, let's just say that it's 45 lines long.
    I love Ruby's structure and convenience functions so much, I often try
    to mirror them in C++. For example:

    void split( std::vector<std::string>* results, const std::string& s,
    const std::string &split_by)
    {
    std::string::size_type start = 0;
    while(1) {
    std::string::size_type found = s.find(split_by, start);
    if (found != start)
    results->push_back( s.substr(start, found - start) );
    if (found == std::string::npos)
    break;
    start = found + split_by.size();
    }
    }

    It's a bit extra work to write such general functions, but think how
    much easier it will be the next time you want to move some code from
    Ruby to C++.

    // Niklas

    Niklas Frykholm Guest

  6. #5

    Default Re: Reason #642 to use Ruby instead of C++ -Carpal Tunnel Syndrome

    Hi!

    * Phil Tomson; 2003-11-17, 14:21 UTC:
    > b.each {|n|
    > if adj_hsh[node].include? n
    > external_cost += 1
    > end
    > }
    >
    > Five lines in Ruby. In C++:
    >
    > for(LI n=b.begin();n!=b.end();++n)
    > {
    > if(_adj_map[node].find(*n) != _adj_map[node].end())
    > {
    > external_cost++;
    > }
    > }
    Now that is *really unfair. Following an alternative scheme of
    writing C(++) programs that are only five lines.

    for(LI n=b.begin(); n != b.end(); ++n) {
    if(_adj_map[node].find(*n) != _adj_map[node].end()) {
    external_cost++;
    }
    }

    I almost always write my C code like that. Note that one can save
    even more lines and write

    for(LI n=b.begin(); n != b.end(); ++n)
    if(_adj_map[node].find(*n) != _adj_map[node].end())
    external_cost++;

    The essential mistake is counting lines. What one should count is the
    number of characters.

    Josef 'Jupp' Schugt
    --
    .-------.
    message > 100 kB? / | |
    sender = spammer? / | R.I.P.|
    text = spam? / ___| |___


    Josef 'Jupp' SCHUGT Guest

  7. #6

    Default Re: Reason #642 to use Ruby instead of C++ -Carpal Tunnel Syndrome

    On Tue, Nov 18, 2003 at 06:47:00AM +0900, Josef 'Jupp' SCHUGT wrote:
    > (...)
    > The essential mistake is counting lines. What one should count is the
    > number of characters.
    Sure.

    for (LI n=b.begin(); n != b.end(); ++n)
    if (_a[nd].find(*n) != _a[nd].end())
    ec++;

    The essential mistake is trying to count something. Go feel & figure.

    -Martin


    Martin Weber Guest

  8. #7

    Default Re: Reason #642 to use Ruby instead of C++ -Carpal Tunnel Syndrome

    On Tue, Nov 18, 2003 at 07:04:19AM +0900, Martin Weber wrote:
    > On Tue, Nov 18, 2003 at 06:47:00AM +0900, Josef 'Jupp' SCHUGT wrote:
    > > (...)
    > > The essential mistake is counting lines. What one should count is the
    > > number of characters.
    >
    > Sure.
    >
    > for (LI n=b.begin(); n != b.end(); ++n)
    > if (_a[nd].find(*n) != _a[nd].end())
    > ec++;
    >
    > The essential mistake is trying to count something. Go feel & figure.
    Somebody proposed the number of tokens as quite a reasonable measurement,
    to be taken with a grain of salt as all metrics.

    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    Not only Guinness - Linux is good for you, too.
    -- Banzai on IRC

    Mauricio Fernández Guest

  9. #8

    Default Re: Reason #642 to use Ruby instead of C++ -Carpal Tunnel Syndrome

    In article <20031118183857.GB18667@student.ei.uni-stuttgart.de>,
    Mauricio Fernández <batsman.geo@yahoo.com> wrote:
    >On Tue, Nov 18, 2003 at 07:04:19AM +0900, Martin Weber wrote:
    >> On Tue, Nov 18, 2003 at 06:47:00AM +0900, Josef 'Jupp' SCHUGT wrote:
    >> > (...)
    >> > The essential mistake is counting lines. What one should count is the
    >> > number of characters.
    >>
    >> Sure.
    >>
    >> for (LI n=b.begin(); n != b.end(); ++n)
    >> if (_a[nd].find(*n) != _a[nd].end())
    >> ec++;
    >>
    >> The essential mistake is trying to count something. Go feel & figure.
    >
    >Somebody proposed the number of tokens as quite a reasonable measurement,
    >to be taken with a grain of salt as all metrics.
    >
    And as I mentioned in the original post, the Ruby snippet is much more
    'comprensible' (readable?) at a glance than the C++ one.

    And just to introduce another measure: productivity. I did the original
    Ruby prototype in 2 hours (including debugging the algorithm) and then set
    out to 'translate' it to C++. It took me a couple of days (well,
    probably about 10 to 12 hours total) to get it all working in C++. Now,
    sure, I don't program in C++ every day so I might be a bit rusty, but
    even if I double my C++ productivity, I'd still be more than 2 to 3X more
    productive in Ruby.

    Phil
    Phil Tomson Guest

  10. #9

    Default Re: Reason #642 to use Ruby instead of C++ -Carpal Tunnel Syndrome

    In article <E1ALasF-0002EQ-AU@fetch-bak.runbox.com>,
    T. Onoma <transami@runbox.com> wrote:
    >Phil wrote:
    >
    >> it's so early?". Of course he said it _wasn't_ alright and I'd be ding'ed
    >> one letter grade if it's not in C or C++.
    >
    >i would have taken the ding and saved the hours. life's too short! oh,
    >maybe had a beer too. :)
    I really thought about it. But then I figured I needed a little C++
    practice anyway (comes up in interviews occasionally :).

    Phil
    Phil Tomson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139