www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 06-05-2005, 06:31 PM
    DJsAC's Avatar
    DJsAC DJsAC is offline
    PHP n00b :D
     
    Join Date: Sep 2003
    Location: Netherlands
    Posts: 353
    Script is more processor intensive than expected, why?

    Hello,
    I was writing a script to calculate the answers to the popular sudoku games...
    my script initializes by making an array with 81 values. In each slot I'm storing the X coordinate, Y coordinate, Q uadrant, and the possible values (1-9)

    Its a pretty straightforward script, and I tried commenting it as much as possible. However when I run it, It will echo this:
    Quote:
    Fatal error: Maximum execution time of 30 seconds exceeded in C:\apache2\htdocs\Sudoku\initialize.php on line 111
    I'll include the code in the page and download as .txt, its 110 lines. (error is on 111 )
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Sudoku Solver</title>

    </head>
    <body>
    <?php
    //Set primary numerical possibilities per cell in the grid.
    $a = 1;
    $b = 2;
    $c = 3;
    $d = 4;
    $e = 5;

    $f = 6;
    $g = 7;
    $h = 8;
    $i = 9;

    $x = 0; //X-axis 1-9th column.
    $y = 0; //Y-axis 1-9th column.
    $q = 1; //Quadrants 1-9

    /* Schematic:

              X123     X456    X789
            |------|------|------|
    Y123    |  Q1  |  Q2  |  Q3  |
            |------|------|------|
    Y456    |  Q4  |  Q5  |  Q6  |
            |------|------|------|
    Y789    |  Q7  |  Q8  |  Q9  |
            |------|------|------|


    More specific:
    Each [] represents a cell for a number.
    Nine cells per quadrant (Q)
    Nine quadrants per grid

         1 2 3    4 5 6    7 8 9
    1    [][][] | [][][] | [][][]
    2    [ Q1 ] | [ Q2 ] | [ Q3 ]
    3    [][][] | [][][] | [][][]
         ------ + ------ + ------
    4    [][][] | [][][] | [][][]
    5    [ Q4 ] | [ Q5 ] | [ Q6 ]
    6    [][][] | [][][] | [][][]
         ------ + ------ + ------
    7    [][][] | [][][] | [][][]
    8    [ Q7 ] | [ Q8 ] | [ Q9 ]
    9    [][][] | [][][] | [][][]

    */

    $z = 1; //simple counter

    $array_numbers = array($a,$b,$c,$d,$e,$f,$g,$h,$i);

    $array_temp = array("X".$x,
                        
    "Y".$y,
                        
    "Q".$q,
                        
    $array_numbers);

    $array_data = array();

    for (
    $x=1; $x<10; $x++)
        {
            for (
    $y=1; $y<10; $y++)
                {
                    
    /*if (($x == 1) OR ($x == 2) OR ($x == 3))
                        {
                            $q = 1;
                        }
                    else*/
    if (($x == 4) OR ($x == 5) OR ($x == 6))
                        {
                            
    $q = 2;
                        }
                    elseif ((
    $x == 7) OR ($x == 8) OR ($x == 9))
                        {
                            
    $q = 3;
                        }
                    
                    
    /*if (($y == 1) OR ($y == 2) OR ($y == 3))
                        {
                            $q = ($q+0);
                        }
                    else*/
    if (($y == 4) OR ($y == 5) OR ($y == 6))
                        {
                            
    $q = ($q+3);
                        }
                    elseif ((
    $y == 7) OR ($y == 8) OR ($y == 9))
                        {
                            
    $q = ($q+6);
                        }
                            
                    
    $data = array("X".$x, "Y".$y, "Q".$q, $array_numbers);
                    
    $array_data[$z] = $data;
                    
    $z++;
                    
    //print_r($data);
                    //print("<hr />");
                
    }
            
    //print("<hr color=\"red\" />");
        
    }
    print(
    "<hr color=\"blue\" />");
    print_r($array_data);
    ?>
    </body>
    </html>
    Why is it so slow? Because I'm calculating the Q value for each y? or is there some other stupid mistake I made and an seeing over the head completely?

    Its supposed to result in a script as follows:
    HTML Code:
    Array
    (
        [1] => Array
            (
                [0] => X1
                [1] => Y1
                [2] => Q1
                [3] => Array
                    (
                        [0] => 1
                        [1] => 2
                        [2] => 3
                        [3] => 4
                        [4] => 5
                        [5] => 6
                        [6] => 7
                        [7] => 8
                        [8] => 9
                    )
    
            )
    
        [2] => Array
            (
                [0] => X1
                [1] => Y2
                [2] => Q1
                [3] => Array
                    (
                        [0] => 1
                        [1] => 2
                        [2] => 3
                        [3] => 4
                        [4] => 5
                        [5] => 6
                        [6] => 7
                        [7] => 8
                        [8] => 9
                    )
    
            )
    .....
    [81] => Array
            (
                [0] => X9
                [1] => Y9
                [2] => Q9
                [3] => Array
                    (
                        [0] => 1
                        [1] => 2
                        [2] => 3
                        [3] => 4
                        [4] => 5
                        [5] => 6
                        [6] => 7
                        [7] => 8
                        [8] => 9
                    )
    
            )
    
    )
    Thank you for taking the time to look !
    Attached Files
    File Type: txt initialize.xhtml.php.txt (2.2 KB, 65 views)
    __________________
    <?php function reply_to_thread($user,$problem=NULL) {
    if($user["nice"] === TRUE) { give_possible_solution($problem);}
    else { exec($user,'code_fail',undefined); }
    return TRUE; } ?>
    Reply With Quote
      #2  
    Old 06-06-2005, 06:44 AM
    DJsAC's Avatar
    DJsAC DJsAC is offline
    PHP n00b :D
     
    Join Date: Sep 2003
    Location: Netherlands
    Posts: 353
    Never Mind.
    Problem solved with ob_start();
    Topic may be closed or whatever the mods prefer.
    __________________
    <?php function reply_to_thread($user,$problem=NULL) {
    if($user["nice"] === TRUE) { give_possible_solution($problem);}
    else { exec($user,'code_fail',undefined); }
    return TRUE; } ?>
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:23 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.