We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!

PHP Nested Loop

I've written a fairly simple piece of PHP that includes 3 nested loops (for loops) and it's now extremely slow, taking several minutes to process.

There's nothing very complex going on, total of about 100 MySQL updates in there, is there a known issue with nested loops in PHP, Google doesn't seem to think so.

Anyone got any ideas, I can post the code if you want to look at it.
:A

Comments

  • RobTang
    RobTang Posts: 1,064 Forumite
    It doesn't really sound that efficient.

    and 100 update statements sounds like a lot ouside of some form of transformation process, which isn't really PHPs job.

    Time how long each section takes to execute and see your bottleneck really is. But its almost certainly your SQL code.
  • The code performs 10,000 MySQL inserts and that takes 2 seconds, it then comes to the 3 nested loops and takes several minutes, including less than 100 MySQL updates.

    Code isn't as efficient as it could be but I don't understand why the huge increase in time, maybe there's some other issue.
    :A
  • DVardysShadow
    DVardysShadow Posts: 18,949 Forumite
    I know very little about this, but let us assume your code does not contain any bug. The kinds of things I would be looking for are
    • Wrong order of nesting, making more work
    • Hopping across tables and back, not allowingg caching to speed things up
    • Unnecessary operations to open and close table and so forth
    Hi, we’ve had to remove your signature. If you’re not sure why please read the forum rules or email the forum team if you’re still unsure - MSE ForumTeam
  • Mr_Oink
    Mr_Oink Posts: 1,012 Forumite
    Chances are, to give you help you'll need to cough up some of that code. Personally, I would try here:
    http://www.codingforums.com/forumdisplay.php?f=6
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 355.1K Banking & Borrowing
  • 254.6K Reduce Debt & Boost Income
  • 455.8K Spending & Discounts
  • 247.9K Work, Benefits & Business
  • 604.9K Mortgages, Homes & Bills
  • 178.8K Life & Family
  • 262.6K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.1K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.