-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,803 additions
and
485 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
function versionCode1(l, A, B) { | ||
return Math.min(2 * A * l, A + 2 * (B - A) * (l - 0.5)); | ||
} | ||
|
||
function versionCode2(l, A, B) { | ||
if (l < 0.5) { | ||
return 2 * A * l; | ||
} else { | ||
return A + 2 * (B - A) * (l - 0.5); | ||
} | ||
} | ||
|
||
function versionCode3(l, A, B) { | ||
return (l < 0.5) ? (2 * A * l) : (A + 2 * (B - A) * (l - 0.5)); | ||
} | ||
|
||
function benchmark(func, iterations, A, B) { | ||
const start = performance.now(); | ||
for (let i = 0; i < iterations; i++) { | ||
const l = Math.random(); | ||
func(l, A, B); | ||
} | ||
const end = performance.now(); | ||
return end - start; | ||
} | ||
|
||
const A = 0.4; | ||
const B = 0.1; | ||
const iterations = 10000000; | ||
|
||
const time1 = benchmark(versionCode1, iterations, A, B); | ||
console.log(`Version Code 1 took ${time1.toFixed(2)} ms`); | ||
|
||
const time2 = benchmark(versionCode2, iterations, A, B); | ||
console.log(`Version Code 2 took ${time2.toFixed(2)} ms`); | ||
|
||
const time3 = benchmark(versionCode3, iterations, A, B); | ||
console.log(`Version Code 3 took ${time3.toFixed(2)} ms`); | ||
|
||
const percentageSlowV1 = ((time1 - time2) / time2) * 100; | ||
const percentageSlowV3 = ((time3 - time2) / time2) * 100; | ||
|
||
console.log(`Version Code 1 is ${percentageSlowV1.toFixed(2)}% slower than Version Code 2`); | ||
console.log(`Version Code 3 is ${percentageSlowV3.toFixed(2)}% slower than Version Code 2`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
function benchmark() { | ||
let iterations = 1000000; // 10 million iterations | ||
let A, B, l; | ||
|
||
// Array of anonymous functions representing the code snippets | ||
const codeSnippets = [ | ||
{ | ||
name: "First Code", | ||
func: () => { | ||
l = Math.random(); // Generate random number between 0 and 1 | ||
A = Math.random(); // Generate random A between 0 and 1 | ||
B = Math.random(); // Generate random B between 0 and 1 | ||
l = l < 0.5 | ||
? 2 * l * (A - B) + B | ||
: 2 * l * (B - A) + (2 * A - B); | ||
} | ||
}, | ||
{ | ||
name: "Second Code", | ||
func: () => { | ||
l = Math.random(); // Generate random number between 0 and 1 | ||
A = Math.random(); // Generate random A between 0 and 1 | ||
B = Math.random(); // Generate random B between 0 and 1 | ||
l = Math.min(2 * l, -2 * l + 2) * (A - B) + B; | ||
} | ||
} | ||
// You can add more code snippets here. | ||
]; | ||
|
||
// Array to store the benchmarking times | ||
let times = []; | ||
|
||
// Loop through each code snippet and benchmark it | ||
codeSnippets.forEach((snippet) => { | ||
console.log(`${snippet.name}:`,snippet.func.toString()); | ||
console.time(snippet.name); | ||
let start = performance.now(); | ||
for (let i = 0; i < iterations; i++) { | ||
snippet.func(); // Execute the anonymous function | ||
} | ||
let end = performance.now(); | ||
let timeTaken = end - start; | ||
console.timeEnd(snippet.name); | ||
|
||
// Store the time taken for this snippet | ||
times.push({ | ||
name: snippet.name, | ||
time: timeTaken | ||
}); | ||
|
||
console.log(`Time taken by ${snippet.name}: ${timeTaken.toFixed(2)} ms`); | ||
}); | ||
|
||
// Sort times array based on performance (ascending order) | ||
times.sort((a, b) => a.time - b.time); | ||
|
||
// Display the sorted order and comparison with the fastest one | ||
console.log("\nPerformance Comparison (Fastest to Slowest):"); | ||
const fastestTime = times[0].time; // Time of the fastest code | ||
times.forEach((snippet, index) => { | ||
let diff = snippet.time - fastestTime; | ||
let percentageDiff = (diff / fastestTime) * 100; | ||
console.log(`${index + 1}. ${snippet.name} - ${snippet.time.toFixed(2)} ms (${percentageDiff.toFixed(2)}% slower than the fastest)`); | ||
}); | ||
|
||
// Show which one is the fastest | ||
console.log(`\n${times[0].name} is the fastest, taking ${fastestTime.toFixed(2)} ms`); | ||
} | ||
|
||
benchmark(); |
51 changes: 51 additions & 0 deletions
51
other-tools-and-tests/benchs/hsla_vs_rgba_val_rendering.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
|
||
function getRandomInt(max) { | ||
return Math.floor(Math.random() * max); | ||
} | ||
|
||
function benchmark(render) { | ||
const start = performance.now(); | ||
for (let i = 0; i < 500000; i++) { | ||
const r = getRandomInt(256); // Random value between 0 and 255 | ||
const g = getRandomInt(256); | ||
const b = getRandomInt(256); | ||
uDark.rgba(r, g, b, 1, render); // Call with random RGB values | ||
} | ||
const end = performance.now(); | ||
return end - start; // Return the time in milliseconds | ||
} | ||
|
||
// Running benchmarks for uDark.rgba_val | ||
const timeRgba = benchmark(uDark.rgba_val); | ||
console.log(`Benchmark for uDark.rgba_val: ${timeRgba.toFixed(2)}ms`); | ||
|
||
// Running benchmarks for uDark.hsla_val | ||
const timeHsla = benchmark(uDark.hsla_val); | ||
console.log(`Benchmark for uDark.hsla_val: ${timeHsla.toFixed(2)}ms`); | ||
|
||
// Ensure both times are valid and determine the faster one | ||
if (typeof timeRgba === 'number' && typeof timeHsla === 'number') { | ||
let faster, slower, renderFaster, renderSlower; | ||
|
||
if (timeRgba < timeHsla) { | ||
faster = timeRgba; | ||
slower = timeHsla; | ||
renderFaster = 'uDark.rgba_val'; | ||
renderSlower = 'uDark.hsla_val'; | ||
} else { | ||
faster = timeHsla; | ||
slower = timeRgba; | ||
renderFaster = 'uDark.hsla_val'; | ||
renderSlower = 'uDark.rgba_val'; | ||
} | ||
|
||
// Calculate the percentage of slowness | ||
const percentageSlowness = ((slower - faster) / faster) * 100; | ||
|
||
console.log(`${renderSlower} is slower by ${percentageSlowness.toFixed(2)}% compared to ${renderFaster}.`); | ||
} else { | ||
console.error("Timing values are not valid."); | ||
} | ||
|
||
} |
Oops, something went wrong.