-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from nekooftheabyss/dev
microoptimization
- Loading branch information
Showing
12 changed files
with
222 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"deno.enable": true | ||
"deno.enable": true, | ||
"deno.unstable": true | ||
} |
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 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 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,24 @@ | ||
import type { GachaChoice } from "../mod.ts"; | ||
|
||
/** | ||
* Roll one from an array of gacha choices. | ||
* @param {GachaChoice[]} choices - Choices to roll from. | ||
* @returns {GachaChoice} Items rolled. | ||
*/ | ||
export function roll<ItemType>( | ||
choices: GachaChoice<ItemType>[], | ||
): GachaChoice<ItemType> { | ||
const total = choices.reduce( | ||
(acc: number, val: GachaChoice<ItemType>) => acc + val.chance, | ||
0, | ||
); | ||
const result = Math.random() * total; | ||
let going = 0.0; | ||
for (let i = 0; i < choices.length; ++i) { | ||
going += choices[i].chance; | ||
if (result < going) { | ||
return choices[i]; | ||
} | ||
} | ||
return choices[Math.floor(Math.random() * choices.length)]; | ||
} |
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,28 @@ | ||
import type { GachaChoice } from "../mod.ts"; | ||
|
||
/** | ||
* Roll one from an array of gacha choices. | ||
* @param {GachaChoice[]} choices - Choices to roll from. | ||
* @returns {GachaChoice} Items rolled. | ||
*/ | ||
export function roll<ItemType>( | ||
choices: GachaChoice<ItemType>[], | ||
): GachaChoice<ItemType> { | ||
let total = 0; | ||
let i = 0; | ||
while (i < choices.length) { | ||
total += choices[i].chance; | ||
i += 1; | ||
} | ||
const result = Math.random() * total; | ||
let going = 0.0; | ||
i = 0; | ||
while (i < choices.length) { | ||
going += choices[i].chance; | ||
if (result < going) { | ||
return choices[i]; | ||
} | ||
i += 1; | ||
} | ||
return choices[Math.floor(Math.random() * choices.length)]; | ||
} |
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,29 @@ | ||
import type { GachaChoice } from "../mod.ts"; | ||
|
||
// Dropped before release since it was slightly faster than v4. | ||
|
||
/** | ||
* Roll one from an array of gacha choices. | ||
* @param {GachaChoice[]} choices - Choices to roll from. | ||
* @returns {GachaChoice} Items rolled. | ||
*/ | ||
export function roll<ItemType>( | ||
choices: GachaChoice<ItemType>[], | ||
): GachaChoice<ItemType> { | ||
let total = 0; | ||
let i = 0; | ||
while (i < choices.length) { | ||
total += choices[i].chance; | ||
i += 1; | ||
} | ||
const result = Math.random() * total; | ||
i -= 1; | ||
while (i > -1) { | ||
total -= choices[i].chance; | ||
if (result > total) { | ||
return choices[i]; | ||
} | ||
i -= 1; | ||
} | ||
return choices[Math.floor(Math.random() * choices.length)]; | ||
} |
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 @@ | ||
import { roll as roll1 } from "../algorithms/v1.ts"; | ||
import { roll as roll2 } from "../algorithms/v2.ts"; | ||
import { roll as roll3 } from "../algorithms/v3.ts"; | ||
import { roll as roll4 } from "../algorithms/v4.ts"; | ||
import { roll as roll4_sub } from "../algorithms/v4_sub.ts"; | ||
import { GachaMachine } from "../mod.ts"; | ||
|
||
import pokemon from "../testdata/pokemon.json" assert { type: "json" }; | ||
|
||
const items = pokemon.slice(0, 151).map((x) => ({ | ||
result: x.id, | ||
chance: x.tier === "legendary" ? 11 : x.tier === "mythic" ? 1 : 25, | ||
})); | ||
|
||
Deno.bench("nop", () => {}); | ||
|
||
Deno.bench("Algorithm V1", () => { | ||
for (let i = 0; i < 1e6; ++i) { | ||
roll1(items); | ||
} | ||
}); | ||
|
||
Deno.bench("Algorithm V2", () => { | ||
for (let i = 0; i < 1e6; ++i) { | ||
roll2(items); | ||
} | ||
}); | ||
|
||
Deno.bench("Algorithm V3", () => { | ||
for (let i = 0; i < 1e6; ++i) { | ||
roll3(items); | ||
} | ||
}); | ||
|
||
Deno.bench("Algorithm V4 _ Sub", () => { | ||
for (let i = 0; i < 1e6; ++i) { | ||
roll4_sub(items); | ||
} | ||
}); | ||
|
||
Deno.bench("Algorithm V4", () => { | ||
for (let i = 0; i < 1e6; ++i) { | ||
roll4(items); | ||
} | ||
}); | ||
|
||
Deno.bench("Algorithm V4 in Fortuna", () => { | ||
for (let i = 0; i < 1e6; ++i) { | ||
GachaMachine.roll(items); | ||
} | ||
}); |
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 @@ | ||
import { roll as roll1 } from "../algorithms/v1.ts"; | ||
import { roll as roll2 } from "../algorithms/v2.ts"; | ||
import { roll as roll3 } from "../algorithms/v3.ts"; | ||
import { roll as roll4 } from "../algorithms/v4.ts"; | ||
import { roll as roll4_sub } from "../algorithms/v4_sub.ts"; | ||
import { GachaMachine } from "../mod.ts"; | ||
|
||
import pokemon from "../testdata/pokemon.json" assert { type: "json" }; | ||
|
||
const items = pokemon.slice(0, 151).map((x) => ({ | ||
result: x.id, | ||
chance: x.tier === "legendary" ? 11 : x.tier === "mythic" ? 1 : 25, | ||
})); | ||
|
||
Deno.bench("nop", () => {}); | ||
|
||
Deno.bench("Algorithm V1", () => { | ||
for (let i = 0; i < 1e2; ++i) { | ||
roll1(items); | ||
} | ||
}); | ||
|
||
Deno.bench("Algorithm V2", () => { | ||
for (let i = 0; i < 1e2; ++i) { | ||
roll2(items); | ||
} | ||
}); | ||
|
||
Deno.bench("Algorithm V3", () => { | ||
for (let i = 0; i < 1e2; ++i) { | ||
roll3(items); | ||
} | ||
}); | ||
|
||
Deno.bench("Algorithm V4 _ Sub", () => { | ||
for (let i = 0; i < 1e2; ++i) { | ||
roll4_sub(items); | ||
} | ||
}); | ||
|
||
Deno.bench("Algorithm V4", () => { | ||
for (let i = 0; i < 1e2; ++i) { | ||
roll4(items); | ||
} | ||
}); | ||
|
||
Deno.bench("Algorithm V4 in Fortuna", () => { | ||
for (let i = 0; i < 1e2; ++i) { | ||
GachaMachine.roll(items); | ||
} | ||
}); |
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,6 @@ | ||
{ | ||
"tasks": { | ||
"bench": "deno bench --unstable benches/100k_rolls.ts", | ||
"test": "deno run --allow-hrtime test.ts" | ||
} | ||
} |
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 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 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