| 20 | * Measures algorithm performance |
| 21 | */ |
| 22 | export function measureAlgorithmPerformance( |
| 23 | algorithm: AlgorithmRunner, |
| 24 | imageData: ImageData, |
| 25 | palette: [number, number, number][], |
| 26 | iterations: number = 3 |
| 27 | ): PerformanceResult { |
| 28 | const times: number[] = []; |
| 29 | |
| 30 | for (let i = 0; i < iterations; i++) { |
| 31 | const start = performance.now(); |
| 32 | algorithm({ |
| 33 | srcData: imageData.data, |
| 34 | width: imageData.width, |
| 35 | height: imageData.height, |
| 36 | params: { |
| 37 | pattern: 0, |
| 38 | threshold: 128, |
| 39 | invert: false, |
| 40 | serpentine: false, |
| 41 | isErrorDiffusion: false, |
| 42 | palette |
| 43 | } |
| 44 | }); |
| 45 | const end = performance.now(); |
| 46 | times.push(end - start); |
| 47 | } |
| 48 | |
| 49 | const avgTime = times.reduce((a, b) => a + b, 0) / times.length; |
| 50 | const pixelCount = imageData.width * imageData.height; |
| 51 | const pixelsPerSecond = (pixelCount / avgTime) * 1000; |
| 52 | |
| 53 | return { |
| 54 | algorithm: algorithm.name || 'unknown', |
| 55 | imageSize: `${imageData.width}×${imageData.height}`, |
| 56 | executionTime: avgTime, |
| 57 | pixelsPerSecond |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Verifies that algorithm output only contains palette colors |