From: Robert Klemme Date: 2005-04-07T17:14:39+09:00 Subject: Re: new! "Boris Glawe" schrieb im Newsbeitrag news:d31gaa$sio$1@newsreader3.netcologne.de... > Robert wrote: > > thanks a lot. > > But what's the difference brtween C++ and Ruby? > > I mean, why i should prefer ruby out of other programming languages? > > Thank you again! > > > > If you program ruby, you spend all your energy in solving your problem and not > in writing a very cryptic language. Ruby is a very clean and easy to read > object oriented language. > > The difference is speed!! I have an algoithm that runs 5 seconds with c++ and 16 > minutes with ruby on my machine. The speed "problem" exists with all skripting > languages (perl,php, etc), it's not ruby's fault. Your Derived#append is suboptimal because it creates new string instances all the time. Try def append(t) @text << t; self end And I'd rewrite Base#increase like this def increase() @var += 1 end It's shorter and it seems to be a tad faster: >> Benchmark.bm(10) do |b| >> @var = 0 >> b.report("+=") { 1000000.times { @var += 1 } } >> @var = 0 >> b.report("= +") { 1000000.times { @var = @var + 1 } } >> end user system total real += 1.125000 0.000000 1.125000 ( 1.132000) = + 1.141000 0.000000 1.141000 ( 1.146000) Kind regards robert > greets Boris > > ########### IN C++: ###################### > > #include > > using namespace std; > > class Base { > > public: > Base(int x) : var(x) > {} > > int getVar() const { return var; } > > void increase() { var++; } > private: > int var; > }; > > > > class Derived : public Base { > public: > Derived(int x, char* t) : Base(x), text(new string(t)) > {} > ~Derived() > { > delete text; > } > > string* getText() const { return text; } > void append(string* t) { > text->append(*t); > } > > private: > string* text; > }; > > > int main (){ > > Derived *d = new Derived(5, "hallo"); > cout << d->getVar() << endl; > > cout << *d->getText() << endl; > > string tmp = "bye"; > d->append(&tmp); > > cout << *d->getText() << endl; > } > > > ############ IN RUBY:######################## > > #!/usr/bin/ruby > > class Base > attr_reader :var > > def initialize(v) > @var = v > end > > def increase > @var = @var + 1 > end > end > > > > class Derived < Base > > attr_reader :text > > def initialize(x, t) > super(x) > @text = t > end > > def append(t) > @text = @text + t > end > > end > > > d = Derived.new(5, "hallo") > d.increase > > print d.var.to_s + "\n" > print d.text + "\n" > > d.append("bye") > > print d.text + "\n"