What you want is for a kernel running outside of your application to forcefully take the CPU from you whenever it feels it necessary to do so. That is True Threading, which Coro is decidedly not.

Your problem in your example code is that your loop is very, very short. You're computing a simple exponential (ok, near the end it's not so simple). The time for Coro/AnyEvent to figure out if there are any other events to process, process them, and come back to you, quite simply overwhelms your loop.

If your loop was doing "real work" in the middle, you might find the overhead of poll/cede to be far less significant.

For example, if your loop was using, say, AnyEvent::HTTP to query a web site, you wouldn't even need to poll/cede. Or if your loop was rendering some Template Toolkit output, that may be heavy enough that ceding each time through the loop may be minor.

The reality is, these things have overhead that they need to go check everything before coming back to where you are. As long as you can push your hard work off into asynchronous pieces of work that need to wait for things (such as sockets, e.g., TCP/IP requests, or user events), Coro will net you a speed up. But if you're doing something computationally trivial between cede's, that's not going to seem like a benefit :-)


In reply to Re: Coro, AnyEvent and performance by Tanktalus
in thread Coro, AnyEvent and performance by mcrose

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.