http://www.perlmonks.org?node_id=911378

elf_firein has asked for the wisdom of the Perl Monks concerning the following question:

I have one subroutine which is running for ithread. The job of this subroutine is to refresh terminal object created through Term::Screen. When all rows of the screen is filled up & new line is added the screen is getting drifted & the topmost line is displayed on screen multiple times while scroll up the screen. I am able to solve this problem following way: 1. Maximum number of number of rows the screen supported 2. Remembering the line which is printed in top-most line of the screen while crossing max row. For that one complete hash need to be remembered for at least for 80 consecutive runs. Sample code is here:

Hash outside the sub routine: %hash = (); // for storing drift line %printable_data = (); // some data are there $terminal = new Term::Screen; $screen_height = $terminal->row; The thread subroutine: sub refresher { $terminal->at(0,0); $terminal->clreos(); foreach (keys %printable_data) { $term->puts($_); . . . if ( $curser_position >= $screen_height ) { $hash{$key} = $_; } } }
If I use closure instead of global variable like %hash, will it reduce the performance? Or it will increase the performance as it is associated with ithread? One more thing, Is my approach is right in order to stop the duplicate print on screen while scrolling? Few more hashes may require to remember data while refreshing the screen in future.

Replies are listed 'Best First'.
Re: Will closure increase performance thread processing subroutine?
by BrowserUk (Patriarch) on Jun 25, 2011 at 17:11 UTC

    There is too little code here to understand how it is used, and too little description to understand what if any problems you might or might not be having.

    You talk about threads but there is no threading shown. If the sub refresher() is being called as a thread procedure, then utilising closures over variables that exist in the calling thread, without also sharing those variables, makes no sense at all. Those closures will be cloned each time you start a new thread, and any changes made to them will not be reflected back into the original hashes, and so will be lost when the thread ends.

    You seem to be worrying about the performance, without realising that the code you are worrying about probably doesn't work. (Almost certainly doesn't, but it is impossible to tell for sure with seeing where, how and why the code is being called.)

    To get any answers that are meaningful(*), you will have to post a cut-down, working demonstration of how you envisage this code working and being used.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Will closure increase performance while used in suroutine of thread?
by ikegami (Patriarch) on Jun 25, 2011 at 17:11 UTC

    If %hash is lexical, then it's already being captured by a closure. Remember,

    sub refresher { }
    is the same thing as
    BEGIN { *refresher = sub { }; }

    If it's not a lexical, you're asking if it's faster to use a lexical or a package variable. Seriously?