Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
sudoku21
(version: 0)
Comparing performance of:
shao vs thre
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [[".",".",".",".",".",".",".",".","."], ["4",".",".",".",".",".",".",".","."], [".",".",".",".",".",".","6",".","."], [".",".",".","3","8",".",".",".","."], [".","5",".",".",".","6",".",".","1"], ["8",".",".",".",".",".",".","6","."], [".",".",".",".",".",".",".",".","."], [".",".","7",".","9",".",".",".","."], [".",".",".","6",".",".",".",".","."]]
Tests:
shao
function sudoku2(grid) { var empty = "."; var chunk = []; for (var i = 0; i < grid.length; i++) { for (var j = 0; j < grid.length; j++) { if (j % 3 === 0 && !checkChunk(i, j)) { return false; } if (checkRowValid(grid[i], grid[i][j], j)) { return false; } if (checkColumnValid(grid, i, j)) { return false; } } } return true; function checkChunk(rowIndex, colIndex) { var counter = rowIndex % 3; while (counter < 3 && (rowIndex + counter) < grid.length) { chunk.push(grid[rowIndex + counter].slice(colIndex, colIndex + 3)); counter++; } chunk = [].concat.apply([], chunk); var isChunkValid = chunk.every(function (item, i) { if (item === empty) { return true; } return chunk.indexOf(item, i + 1) <= 1; }); chunk = []; return isChunkValid; } function checkRowValid(row, item, rowIndex) { return item !== empty && row.indexOf(item, rowIndex + 1) !== -1; } function checkColumnValid(grid, rowIndex, colIndex) { var entries = 0; for (var row = 0; row < grid.length; row++) { if (grid[row][colIndex] !== empty && grid[rowIndex][colIndex] === grid[row][colIndex]) { entries++; } } return entries > 1; } } sudoku2(data);
thre
function sudoku2(grid) { for(var x = 0; x < grid.length; x++) { var checkRow = {}; var checkColumn = {}; var checkSquare = {}; var offsetX = Math.floor(x/3) * 3; var offsetY = (x % 3) * 3; for(var y = 0; y < grid.length; y++) { var squareX = Math.floor(y/3) + offsetX; var squareY = (y % 3) + offsetY; if (isBroken(checkRow, grid[x][y]) || isBroken(checkColumn, grid[y][x]) || isBroken(checkSquare, grid[squareX][squareY])) { return false; } } } function isBroken(check, value) { return (check[value] && value !== '.') || !(check[value] = true); } return true; }; sudoku2(data);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
shao
thre
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and its components for you. **Benchmark Overview** The provided benchmark is a JavaScript test case that measures the performance of Sudoku grid validation. The goal is to determine whether a given 9x9 Sudoku grid is valid or not. **Options Compared** There are two distinct approaches compared in this benchmark: 1. **Approach 1: "shao"** This approach uses a more traditional and explicit method for validating the Sudoku grid. It: * Iterates through each row, column, and sub-grid (3x3 squares) to check for validity. * Uses separate objects (`checkRow`, `checkColumn`, and `checkSquare`) to store the values of each cell in the current row, column, and sub-grid, respectively. * Checks if a value is already present in its corresponding row, column, or sub-grid using an "isBroken" function. If it's not, but should be (i.e., it's a valid Sudoku number), the approach returns false. 2. **Approach 2: "thre"** This approach uses a more concise and optimized method for validating the Sudoku grid. It: * Uses a single loop to iterate through each row and cell. * Creates separate objects (`checkRow`, `checkColumn`, and `checkSquare`) to store the values of each cell in the current row, column, and sub-grid, respectively. * Uses an "isBroken" function to check if a value is already present in its corresponding row, column, or sub-grid. If it's not, but should be (i.e., it's a valid Sudoku number), the approach returns false. **Pros and Cons of Each Approach** **Approach 1: "shao"** Pros: * More explicit and intuitive for understanding the validation logic. * May be more suitable for developers who prefer a more traditional approach to problem-solving. Cons: * More verbose code, which can increase compilation and runtime overhead. * May require more memory allocation and garbage collection due to the use of separate objects for each row, column, and sub-grid. **Approach 2: "thre"** Pros: * More concise and compact code, which can reduce compilation and runtime overhead. * May be more suitable for developers who prefer a more optimized and efficient approach to problem-solving. Cons: * Less explicit and less intuitive than the traditional approach. * May require more complex logic to understand the validation rules and edge cases. **Other Considerations** * **Memory Allocation**: Both approaches allocate memory for each row, column, and sub-grid. However, Approach 2 uses a more compact representation of the Sudoku grid using separate objects, which may reduce memory allocation overhead. * **Cache Performance**: The compact representation used in Approach 2 may improve cache performance due to its smaller size and more sequential access pattern. **Benchmark Results** The provided benchmark results show that: * "shao" approach performs better (44648.5390625 executions per second) compared to the "thre" approach (31373.638671875 executions per second). However, it's essential to note that the results may depend on various factors such as system architecture, compiler optimizations, and interpreter behavior.
Related benchmarks:
using .length within and out of for loop
sudoku2
Table appending
comparing coercion string vs number
Comments
Confirm delete:
Do you really want to delete benchmark?