+ Reply to Thread
Results 1 to 8 of 8

Improve code performance for Sudoku

  1. #1
    Registered User
    Join Date
    05-27-2024
    Location
    southeast Michigan
    MS-Off Ver
    2010
    Posts
    30

    Question Improve code performance for Sudoku

    I wrote a spreadsheet to help me with Killer Sudoku. The spreadsheet has nine rows which are used to input the numbers seen in (up to) nine cells and a cell for the total value. The VBA macro starts by reading everything in - the numbers are read into the two-dimensional array vals, where the first index is the row number, the second index is the nth value in the row, and the array cnt holds the number of numbers in the row. The values are added and if the sum equals the total value, the numbers in the cells are stored. Following is the 'complex' code that does this:
    Please Login or Register  to view this content.
    The program works without issue, but it can become excruciating slow. I just did this experiment to quantify the problem. I started with two rows of values and worked my way up. Each row had all nine numbers and the total was 5*number of rows. I then timed how long it took the macros to complete. Here are the results:
    • 2 cells, sum 10: < 1 sec
    • 3 cells, sum 15: < 1 sec
    • 4 cells, sum 20: 1 sec
    • 5 cells, sum 25: 2 sec
    • 6 cells, sum 30: 45 sec

    I didn't bother going to 7 rows because I would still be waiting for things to complete. Why on earth is this taking so long? I can attach the actual spreadsheet is anyone cares.
    Last edited by 6StringJazzer; 05-31-2024 at 06:47 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,005

    Re: Why is Excel VBA so slow?

    What are the values in cnt(1) through cnt(9)? It would be better if you showed us all the code. It would be even better to attach your file so we can run the code. We can't run this excerpt.

    If you are doing an exhaustive iteration, the number of passes through the loop grows exponentially. You get up to big numbers really fast.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,005

    Re: Why is Excel VBA so slow?

    I am updating your title because VBA isn't slow; this particular code is slow.

  4. #4
    Registered User
    Join Date
    05-27-2024
    Location
    southeast Michigan
    MS-Off Ver
    2010
    Posts
    30

    Re: Improve code performance for Sudoku

    Yes, I know that the number of passes through the loop grows exponentially, but the leap seems beyond believable.
    Attached Files Attached Files

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,005

    Re: Improve code performance for Sudoku

    I can't figure out how this whole thing works, but if I fill numbers in B1:J9, then every element of array cnt is 9, which gives you over 387 million operations to perform, times the 9 lines of code in that operation. So that's going to take a while. Let me know if I misunderstand how your code works.

  6. #6
    Registered User
    Join Date
    05-27-2024
    Location
    southeast Michigan
    MS-Off Ver
    2010
    Posts
    30

    Re: Improve code performance for Sudoku

    You are absolutely correct, but the actual performance makes no sense to me. When I put numbers in B1:J5, this means that about ~59,000 operations are needed and executing took 2 seconds. However, when I put numbers in B1:J6, which is just nine times as many operations, executing took 45 seconds, basically three times longer than expected. This makes no sense!

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,005

    Re: Improve code performance for Sudoku

    I was looking at this again today but still have no clue as to how to set up your data for a run. Please give instructions for how to enter data on Sheet1 to get this to run. I know it's been nearly a month so if you're no longer interested, let me know that too.

  8. #8
    Forum Contributor
    Join Date
    10-02-2012
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    293

    Re: Improve code performance for Sudoku

    I'm pretty sure there are recursive ways to do this. So many nested loops throughout the modules. I will take a look and see if I can suggest anything, but as Jazza said, need more info on the set up.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 07-31-2020, 07:38 PM
  2. Slow macro, slow pc or wrong code?
    By corky81 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-24-2018, 05:58 AM
  3. Replies: 0
    Last Post: 06-06-2018, 03:54 AM
  4. Slow saving in Excel 2003 format from Excel 2010
    By simonc2 in forum Excel General
    Replies: 0
    Last Post: 10-18-2013, 08:40 AM
  5. Macro is running real slow and makes navigating the worksheet really slow after execution.
    By MichWolverines in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2013, 04:29 PM
  6. [SOLVED] slow computer (slow clipboard) breaks my macro
    By twilsonco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2013, 09:16 PM
  7. Excel 2003 macro working very slow in Excel 2007 (xlsm format)
    By kapil in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-17-2010, 11:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1