Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6806043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:39:42+00:00 2026-05-26T19:39:42+00:00

I’ve been told that a duff device doesn’t work with PHP, because the switch

  • 0

I’ve been told that a duff device doesn’t work with PHP, because the switch and case construct working different. I’ve found this duff devive on php.net, my question is what is wrong with this device? Or didn’t I understand a duff device? In my assembler I can unroll a loop with a simple command and when it compiles I get an unrolled loop.

<?php
$n = $ITERATIONS % 8;
while ($n--) $val++;
$n = (int)($ITERATIONS / 8);
while ($n--) {
   $val++;
   $val++;
   $val++;
   $val++;
   $val++;
   $val++;
   $val++;
   $val++;
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:39:43+00:00Added an answer on May 26, 2026 at 7:39 pm

    That is not a Duff’s Device. It uses a special pre loop alignment step (which is precisely what Duff’s Device is designed to avoid).

    In a true Duff’s Device there is a single section of unrolled code which is initially partially skipped over by a switch. This trick reduces the required amount of code (to just the loop) and reduces the number of conditional jumps in the code.

    The code that you presented is simply a manually unrolled loop.


    Loop unrolling:

    Loop unrolling is an optimisation technique in which several iterations of a loop are processed at once. So instead of:

    $number_of_iterations = 128;
    for ($n = 0; $n !== $number_of_iterations; ++$n) {
        do_something();
    }
    

    You use:

    $number_of_iterations = 128;
    for ($n = 0; $n !== (int)($number_of_iterations / 4); ++$n) {
        //Repeat do_something() four times.
        //Four is the "unrolling factor".
        do_something();
        do_something();
        do_something();
        do_something();
    }
    

    The advantage of this is speed. Conditional branching is typically a relatively expensive operation. Compared to the unrolled loop, the first loop will pass over the conditional branch four times more often.

    Unfortunately, this approach is somewhat problematic. Suppose $number_of_iterations was not divisible by four – the division of labour into larger chunks would no longer work. The traditional solution to this is to have another loop which performs the work in smaller chunks until the remaining amount of work can be performed by an unrolled loop:

    $number_of_iterations = 130;
    //Reduce the required number of iterations
    //down to a value that is divisible by 4
    while ($number_of_iterations % 4 !== 0) {
        do_something();
        --$number_of_iterations
    }
    //Now perform the rest of the iterations in an optimised (unrolled) loop.
    for ($n = 0; $n !== (int)($number_of_iterations / 4); ++$n) {
        do_something();
        do_something();
        do_something();
        do_something();
    }
    

    This is better, but the initial loop is still needlessly inefficient. It again is branching at every iteration – an expensive proposition. In php, this is as good as you can get (??).

    Now enter Duff’s Device.

    Duffs Device:

    Instead of performing a tight loop before entering the efficient unrolled zone, another alternative is to go straight to the unrolled zone, but to initially jump to part way through the loop. This is called Duff’s Device.

    I will now switch the language to C, but the structure of the code will remain very similar:

    //Note that number_of_iterations
    //must be greater than 0 for the following code to work
    int number_of_iterations = 130;
    //Integer division truncates fractional parts
    //counter will have the value which corresponds to the
    //number of times that the body of the `do-while`
    //will be entered.
    int counter = (number_of_iterations + 3) / 4;
    switch (number_of_iterations % 4) {
        case 0: do { do_something();
        case 3:      do_something();
        case 2:      do_something();
        case 1:      do_something();
                while (--counter > 0)
    }
    

    All of the conditional branches in the while ($number_of_iterations % 4 !== 0) from earlier have been replaced by a single computed jump (from the switch).


    This whole analysis is predicated on the flawed notions that reducing the number of conditional branches in a region of code will always result in significantly better performance and that the compiler will not be able to perform these sorts of micro-optimisations by itself where appropriate. Both manual loop unrolling and Duff’s Device should be avoided in modern code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.