objects vs sub routines in loop

Discussion in 'Perl Misc' started by ed, Jun 29, 2003.

  1. ed

    ed Guest

    Hi all. I need to create a script that is always going to be running.
    So I guess pretty much everything is going to be taking place inside
    of
    one main loop.

    If I use objects that are lexically scoped within that loop, it means
    that
    new objects have to be created at the beginning of every iteration.

    Would using objects be much slower than just calling sub routines?

    If the answer to that is yes. I wonder if one solution(as a way to
    still use OOP)
    would be to extend my classes and create a "clear()" method or
    something similar
    that would basically set my object back to an uninitialized state.
    Then I reinitialize it at the top of the loop and use it as if it were
    a new
    object.

    Example:

    my $obj = new Obj;
    while (!$done)
    {
    $obj->initialize('param1', 'param2');

    $obj->doSomething();
    $obj->doSomethingElse();

    $obj->clear();
    }

    As opposed to:

    while (!$done)
    {
    my $obj = new Obj('param1', 'param2');

    $obj->doSomething();
    $obj->doSomethingElse();
    }

    -----
    Note: In a real situation the parameters to the objects will change
    with
    every interation. I just wanted to keep the examples simple.

    ----------

    Would using the first example speed things up substantially?
    Would it still be substantially slower than if I were just calling sub
    routines
    instead of creating objects?

    Is object creation quick enough in perl that I should not even worry
    about it?

    tia,
    --ed
     
    ed, Jun 29, 2003
    #1
    1. Advertising

  2. ed

    Bob Walton Guest

    ed wrote:

    > Hi all. I need to create a script that is always going to be running.
    > So I guess pretty much everything is going to be taking place inside
    > of
    > one main loop.
    >
    > If I use objects that are lexically scoped within that loop, it means
    > that
    > new objects have to be created at the beginning of every iteration.
    >
    > Would using objects be much slower than just calling sub routines?
    >
    > If the answer to that is yes. I wonder if one solution(as a way to
    > still use OOP)
    > would be to extend my classes and create a "clear()" method or
    > something similar
    > that would basically set my object back to an uninitialized state.
    > Then I reinitialize it at the top of the loop and use it as if it were
    > a new
    > object.
    >
    > Example:
    >
    > my $obj = new Obj;
    > while (!$done)
    > {
    > $obj->initialize('param1', 'param2');
    >
    > $obj->doSomething();
    > $obj->doSomethingElse();
    >
    > $obj->clear();
    > }
    >
    > As opposed to:
    >
    > while (!$done)
    > {
    > my $obj = new Obj('param1', 'param2');
    >
    > $obj->doSomething();
    > $obj->doSomethingElse();
    > }
    >
    > -----
    > Note: In a real situation the parameters to the objects will change
    > with
    > every interation. I just wanted to keep the examples simple.
    >
    > ----------
    >
    > Would using the first example speed things up substantially?
    > Would it still be substantially slower than if I were just calling sub
    > routines
    > instead of creating objects?
    >
    > Is object creation quick enough in perl that I should not even worry
    > about it?
    >

    ....
    > --ed


    Why don't you

    use Benchmark;

    and find out the answers to your speed questions on your own objects?
    In my opinion, if you are really really concerned about speed, you
    should probably code in C and forget OO altogether. It will probably be
    the method calls that slow things down the most, regardless of the OO
    language you code in. But if your methods themselves do significant
    work, the overhead of the method calls could well be inconsequential.

    It seems like you are putting too much emphasis on speed -- by the time
    you complete your project, Moore's law will have made your speed
    problems go away anyhow.

    --
    Bob Walton
     
    Bob Walton, Jun 29, 2003
    #2
    1. Advertising

  3. ed

    ed Guest

    On Sun, 29 Jun 2003 19:44:43 GMT, Bob Walton
    <> wrote:
    >Why don't you
    >
    > use Benchmark;
    >
    >and find out the answers to your speed questions on your own objects?



    I'm just starting on writing the OO stuff, so I thought maybe I should
    ask before I write it all in an OO style.
    I've really just recently started getting into OOP and I think
    I'm really starting to like it. So ideally I'd like to use the OO
    approach
    for the program. But if the speed penalty was great, I could do
    without object
    oriented code.


    >It seems like you are putting too much emphasis on speed -- by the time
    >you complete your project, Moore's law will have made your speed
    >problems go away anyhow.


    You're probably right.
    I think I'll just go ahead and use OOP for everything I wanted, in the
    way
    that seems natural(confining the object to the scope of the loop).
    Then I'll benchmark and see if it seems like it should be faster, then
    I'll worry about what
    to do about it.

    thanks,
    --ed
     
    ed, Jun 30, 2003
    #3
  4. ed

    llewelly Guest

    ed <> writes:

    > On Sun, 29 Jun 2003 19:44:43 GMT, Bob Walton
    > <> wrote:
    >>Why don't you
    >>
    >> use Benchmark;
    >>
    >>and find out the answers to your speed questions on your own objects?

    >
    >
    > I'm just starting on writing the OO stuff, so I thought maybe I should
    > ask before I write it all in an OO style.
    > I've really just recently started getting into OOP and I think
    > I'm really starting to like it. So ideally I'd like to use the OO
    > approach
    > for the program. But if the speed penalty was great, I could do
    > without object
    > oriented code.

    [snip]

    w.r.t to performance of OO code, the best thing you can do is think
    about whether OO is appropriate for your design. Use it where it fits.

    if you need OO, you need OO, and emulating OO without using OO
    language constructs is not likely to be faster than using OO
    constructs directly.

    To make an analogy, an array is faster than a hash if you don't need
    key => value mappings, but if you do, the array just won't do the
    job, and implementing a hash table by hand is not likely to result
    in faster code than perl's builtin hashes.

    Don't use OO where it's inappropriate - but recall that when you need
    it, trying to wriggle out of the need is not likely to buy you
    performance.
     
    llewelly, Jul 1, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. THY
    Replies:
    1
    Views:
    401
    Steve C. Orr, MCSD
    Aug 19, 2003
  2. Ben
    Replies:
    2
    Views:
    919
  3. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    2,083
    Lawrence D'Oliveiro
    May 20, 2011
  4. Isaac Won
    Replies:
    9
    Views:
    397
    Ulrich Eckhardt
    Mar 4, 2013
  5. Replies:
    8
    Views:
    316
    Jorgen Grahn
    May 16, 2013
Loading...

Share This Page