简体   繁体   English

javascript非递归置换算法性能

[英]javascript non-recursive permutation algorithm performance

if (tl;dr) {

goto https://jsfiddle.net/y5v0ur4p/60/ 转到https://jsfiddle.net/y5v0ur4p/60/

Any ideas on how to run this permutation-pattern faster? 关于如何更快运行此排列模式的任何想法?

} else {

I was wondering if it was possible to write a non-recursive permutation function in javascript that could keep up with the performance of recursive ones (eg Heap's algorithm). 我想知道是否有可能在javascript中编写一个非递归置换函数,以跟上递归函数的性能(例如,Heap的算法)。 After a couple of weeks I had an idea which worked out pretty well so far. 几周后,我有了一个想法,到目前为止效果很好。 Here's the explanation https://jsfiddle.net/u68wyvzk/6/ 这是解释https://jsfiddle.net/u68wyvzk/6/

In case the explanation left something unclear, just ask :) } 如果解释不清楚,请问:) }

It is always possible to eliminate recursion by manually implementing a stack that would otherwise be implicitly handled by the JavaScript engine. 始终可以通过手动实现堆栈来消除递归,否则该堆栈将由JavaScript引擎隐式处理。 Making the stack explicit allows for some optimizations (as we don't need to store the whole call stack and can eliminate function calls from within inner loops) and is often faster even though the computational complexity remains the same. 使堆栈显式表示可以进行一些优化(因为我们不需要存储整个调用堆栈,并且可以消除内部循环中的函数调用),并且即使计算复杂度保持不变,也通常更快。

See https://stackoverflow.com/a/37580979/1647737 for a performant non-recursive implementation of Heap's algorithm. 有关Heap算法的高性能非递归实现,请参见https://stackoverflow.com/a/37580979/1647737

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM