Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: speed up compile by map#713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
wwwzbwcom wants to merge1 commit intogpujs:develop
base:develop
Choose a base branch
Loading
fromwwwzbwcom:speed-up-compile-by-map

Conversation

wwwzbwcom
Copy link

speed up compile by using map instead of array
map is more than 30x faster

const mathFunctionsMap = {  abs: true,  acos: true,  acosh: true,  asin: true,  asinh: true,  atan: true,  atan2: true,  atanh: true,  cbrt: true,  ceil: true,  clz32: true,  cos: true,  cosh: true,  expm1: true,  exp: true,  floor: true,  fround: true,  imul: true,  log: true,  log2: true,  log10: true,  log1p: true,  max: true,  min: true,  pow: true,  random: true,  round: true,  sign: true,  sin: true,  sinh: true,  sqrt: true,  tan: true,  tanh: true,  trunc: true,};function isAstMathFunctionMap(ast) {  return !!mathFunctionsMap[ast];}function isAstMathFunctionList(ast) {  const mathFunctions = [    "abs",    "acos",    "acosh",    "asin",    "asinh",    "atan",    "atan2",    "atanh",    "cbrt",    "ceil",    "clz32",    "cos",    "cosh",    "expm1",    "exp",    "floor",    "fround",    "imul",    "log",    "log2",    "log10",    "log1p",    "max",    "min",    "pow",    "random",    "round",    "sign",    "sin",    "sinh",    "sqrt",    "tan",    "tanh",    "trunc",  ];  return mathFunctions.indexOf(ast) > -1;}console.time("isAstMathFunctionMap");// console.log(isAstMathFunctionSet(""));// console.log(isAstMathFunctionSet("trunc"));for (let i = 0; i < 1e8; i++) {    isAstMathFunctionMap("trunc")}console.timeEnd("isAstMathFunctionMap");console.time("isAstMathFunctionList");for (let i = 0; i < 1e8; i++) {  isAstMathFunctionList("trunc");}console.timeEnd("isAstMathFunctionList");/*isAstMathFunctionMap: 75.357msisAstMathFunctionList: 3.747s*/

@harshkhandeparkar
Copy link

It's taking longer because you are defining the array in each call. If the array is a constant, the array is actually faster.

@wwwzbwcom
Copy link
Author

@harshkhandeparkar This doesnt affect much, array is still much slower:

Also, in the source code we defining array in function, so I do this in the test

const mathFunctionsMap = {  abs: true,  acos: true,  acosh: true,  asin: true,  asinh: true,  atan: true,  atan2: true,  atanh: true,  cbrt: true,  ceil: true,  clz32: true,  cos: true,  cosh: true,  expm1: true,  exp: true,  floor: true,  fround: true,  imul: true,  log: true,  log2: true,  log10: true,  log1p: true,  max: true,  min: true,  pow: true,  random: true,  round: true,  sign: true,  sin: true,  sinh: true,  sqrt: true,  tan: true,  tanh: true,  trunc: true,};function isAstMathFunctionMap(ast) {  return !!mathFunctionsMap[ast];}const mathFunctions = [    "abs",    "acos",    "acosh",    "asin",    "asinh",    "atan",    "atan2",    "atanh",    "cbrt",    "ceil",    "clz32",    "cos",    "cosh",    "expm1",    "exp",    "floor",    "fround",    "imul",    "log",    "log2",    "log10",    "log1p",    "max",    "min",    "pow",    "random",    "round",    "sign",    "sin",    "sinh",    "sqrt",    "tan",    "tanh",    "trunc",  ];function isAstMathFunctionList(ast) {  return mathFunctions.indexOf(ast) > -1;}console.time("isAstMathFunctionMap");// console.log(isAstMathFunctionSet(""));// console.log(isAstMathFunctionSet("trunc"));for (let i = 0; i < 1e8; i++) {    isAstMathFunctionMap("trunc")}console.timeEnd("isAstMathFunctionMap");console.time("isAstMathFunctionList");for (let i = 0; i < 1e8; i++) {  isAstMathFunctionList("trunc");}console.timeEnd("isAstMathFunctionList");/*isAstMathFunctionMap: 66.243msisAstMathFunctionList: 3.532s*/

@wwwzbwcom
Copy link
Author

Checking the first element by indexOf is also slower:

const mathFunctionsMap = {  abs: true,  acos: true,  acosh: true,  asin: true,  asinh: true,  atan: true,  atan2: true,  atanh: true,  cbrt: true,  ceil: true,  clz32: true,  cos: true,  cosh: true,  expm1: true,  exp: true,  floor: true,  fround: true,  imul: true,  log: true,  log2: true,  log10: true,  log1p: true,  max: true,  min: true,  pow: true,  random: true,  round: true,  sign: true,  sin: true,  sinh: true,  sqrt: true,  tan: true,  tanh: true,  trunc: true,};function isAstMathFunctionMap(ast) {  return !!mathFunctionsMap[ast];}const mathFunctions = [    "abs",    "acos",    "acosh",    "asin",    "asinh",    "atan",    "atan2",    "atanh",    "cbrt",    "ceil",    "clz32",    "cos",    "cosh",    "expm1",    "exp",    "floor",    "fround",    "imul",    "log",    "log2",    "log10",    "log1p",    "max",    "min",    "pow",    "random",    "round",    "sign",    "sin",    "sinh",    "sqrt",    "tan",    "tanh",    "trunc",  ];function isAstMathFunctionList(ast) {  return mathFunctions.indexOf(ast) > -1;}console.time("isAstMathFunctionMap");// console.log(isAstMathFunctionSet(""));// console.log(isAstMathFunctionSet("trunc"));for (let i = 0; i < 1e8; i++) {    isAstMathFunctionMap("abs")}console.timeEnd("isAstMathFunctionMap");console.time("isAstMathFunctionList");for (let i = 0; i < 1e8; i++) {  isAstMathFunctionList("abs");}console.timeEnd("isAstMathFunctionList");/*const mathFunctionsMap = {  abs: true,  acos: true,  acosh: true,  asin: true,  asinh: true,  atan: true,  atan2: true,  atanh: true,  cbrt: true,  ceil: true,  clz32: true,  cos: true,  cosh: true,  expm1: true,  exp: true,  floor: true,  fround: true,  imul: true,  log: true,  log2: true,  log10: true,  log1p: true,  max: true,  min: true,  pow: true,  random: true,  round: true,  sign: true,  sin: true,  sinh: true,  sqrt: true,  tan: true,  tanh: true,  trunc: true,};function isAstMathFunctionMap(ast) {  return !!mathFunctionsMap[ast];}const mathFunctions = [    "abs",    "acos",    "acosh",    "asin",    "asinh",    "atan",    "atan2",    "atanh",    "cbrt",    "ceil",    "clz32",    "cos",    "cosh",    "expm1",    "exp",    "floor",    "fround",    "imul",    "log",    "log2",    "log10",    "log1p",    "max",    "min",    "pow",    "random",    "round",    "sign",    "sin",    "sinh",    "sqrt",    "tan",    "tanh",    "trunc",  ];function isAstMathFunctionList(ast) {  return mathFunctions.indexOf(ast) > -1;}console.time("isAstMathFunctionMap");// console.log(isAstMathFunctionSet(""));// console.log(isAstMathFunctionSet("trunc"));for (let i = 0; i < 1e8; i++) {    isAstMathFunctionMap("abs")}console.timeEnd("isAstMathFunctionMap");console.time("isAstMathFunctionList");for (let i = 0; i < 1e8; i++) {  isAstMathFunctionList("abs");}console.timeEnd("isAstMathFunctionList");/*isAstMathFunctionMap: 74.071msisAstMathFunctionList: 375.602ms*/

@harshkhandeparkar
Copy link

I did a little more testing too. I found that usingarray.includes is much faster thanindexOf. And theincludes is faster than using an object.

findArrIncludes: 512msfindArrIndexOf: 18376msfindObj: 5201msfindMap: 19002ms

@harshkhandeparkar
Copy link

harshkhandeparkar commentedSep 3, 2021
edited
Loading

I used 10^9 operations here and also used a key in between the array (clz32) instead of at last.
Here's the code:
final code attached below

@harshkhandeparkar
Copy link

harshkhandeparkar commentedSep 3, 2021
edited
Loading

Further testing reveals how the time taken changes when the element to be searched changes:

findArrIncludes_first: 561msfindArrIncludes_middle: 605msfindArrIncludes_end: 631msfindArrIndexOf_first: 8968msfindArrIndexOf_middle: 22198msfindArrIndexOf_end: 37412msfindObj_first: 4891msfindObj_middle: 24635msfindObj_end: 25857msfindMap_first: 11336msfindMap_middle: 20039msfindMap_end: 9927ms

indexOf andObject both take more time to search an element at the end than at the beginning.includes on the other hand, takes almost the same amount of time.Map.has is inconsistent for some reason.

@harshkhandeparkar
Copy link

harshkhandeparkar commentedSep 3, 2021
edited
Loading

@harshkhandeparkar This doesnt affect much, array is still much slower:

Somehow, my initial test gave different results. The later tests are consistent with yours. Nice find! Although, changing toincludes might increase the performance even further.

@wwwzbwcom
Copy link
Author

@harshkhandeparkar This doesnt affect much, array is still much slower:

Somehow, my initial test gave different results. The later tests are consistent with yours. Nice find! Although, changing toincludes might increase the performance even further.

If you swap the order of thefindArrIncludes andfindObj,findObj will be faster andfindArrIncludes will be slower, and if test them seperatly, they have similar speed, but they are always much more faster thanfindArrIndexOf

@harshkhandeparkar
Copy link

If you swap the order of the findArrIncludes and findObj, findObj will be faster and findArrIncludes will be slower

Why?

@wwwzbwcom
Copy link
Author

If you swap the order of the findArrIncludes and findObj, findObj will be faster and findArrIncludes will be slower

Why?

Maybe gc or cache related?

@harshkhandeparkar
Copy link

If you swap the order of the findArrIncludes and findObj, findObj will be faster and findArrIncludes will be slower

Why?

Maybe gc or cache related?

Hmm, perhaps. I tested them separately and found that includes still has the benefit of constant search time.

findArrIncludes_first 470 msfindArrIncludes_middle 463 msfindArrIncludes_end 235 ms

and

findObj_first 472 msfindObj_middle 17960 msfindObj_end 19666 ms

@harshkhandeparkar
Copy link

attaching the code as a file here and deleting the above snippets.
speed-test.js.txt

wwwzbwcom reacted with thumbs up emoji

@wwwzbwcom
Copy link
Author

Hmm, perhaps. I tested them separately and found that includes still has the benefit of constant search time.

findArrIncludes_first 470 msfindArrIncludes_middle 463 msfindArrIncludes_end 235 ms

and

findObj_first 472 msfindObj_middle 17960 msfindObj_end 19666 ms

Can confirm this is true, thanks a lot for your help!

lets switch toincludes

harshkhandeparkar reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@wwwzbwcom@harshkhandeparkar

[8]ページ先頭

©2009-2025 Movatter.jp