Performance 如何翻译这个范围编码C++;执行Haskell的代码片段?
我知道足够多的Haskell来翻译下面的代码,但我不知道如何使其运行良好:Performance 如何翻译这个范围编码C++;执行Haskell的代码片段?,performance,haskell,Performance,Haskell,我知道足够多的Haskell来翻译下面的代码,但我不知道如何使其运行良好: typedef unsigned long precision; typedef unsigned char uc; const int kSpaceForByte = sizeof(precision) * 8 - 8; const int kHalfPrec = sizeof(precision) * 8 / 2; const precision kTop = ((precision)1) << kS
typedef unsigned long precision;
typedef unsigned char uc;
const int kSpaceForByte = sizeof(precision) * 8 - 8;
const int kHalfPrec = sizeof(precision) * 8 / 2;
const precision kTop = ((precision)1) << kSpaceForByte;
const precision kBot = ((precision)1) << kHalfPrec;
//This must be called before encoding starts
void RangeCoder::StartEncode(){
_low = 0;
_range = (precision) -1;
}
/*
RangeCoder does not concern itself with models of the data.
To encode each symbol, you pass the parameters *cumFreq*, which gives
the cumulative frequency of the possible symbols ordered before this symbol,
*freq*, which gives the frequency of this symbol. And *totFreq*, which gives
the total frequency of all symbols.
This means that you can have different frequency distributions / models for
each encoded symbol, as long as you can restore the same distribution at
this point, when restoring.
*/
void RangeCoder::Encode(precision cumFreq, precision freq, precision totFreq){
assert(cumFreq + freq <= totFreq && freq && totFreq <= kBot);
_low += cumFreq * (_range /= totFreq);
_range *= freq;
while ((_low ^ _low + _range) < kTop or
_range < kBot and ((_range= -_low & kBot - 1), 1)){
//the "a or b and (r=..,1)" idiom is a way to assign r only if a is false.
OutByte(_low >> kSpaceForByte); //output one byte.
_range <<= sizeof(uc) * 8;
_low <<= sizeof(uc) * 8;
}
}
typedef无符号长精度;
typedef无符号字符uc;
常量int kSpaceForByte=sizeof(精度)*8-8;
const int kHalfPrec=尺寸(精度)*8/2;
常数精度kTop=((精度)1)
我在某个地方读到,显式递归在ghc上的性能往往很差
不可以。GHC为递归生成的机器代码很慢,无法减少(或者GHC“不想”减少)。如果递归可以展开(我在你的代码段中没有看到任何基本问题),它被转换成几乎相同的机器代码,如C或C++中的while循环。
假设我在64位平台上运行,我应该使用未绑定的Word64参数吗?如果我将精度降低到32位,压缩质量不会显著降低。GHC会为此进行更好的优化吗
你是说?让GHC来处理它,使用装箱类型。我从来没有遇到过这样的情况:只有使用非固定类型才能获得利润。在64位平台上使用32位类型没有帮助
优化GHC性能的一个一般规则是尽可能避免数据结构。如果您可以通过函数参数或闭包传递数据,请利用这个机会 请重新格式化代码以适应问题列宽。我认为只有注释突出,但我会修正!我还要提到严格注释(bang模式)和避免过度使用thunks@jozefg是的,这是一种帮助GHC表现良好的技术。未装箱的向量不是比装箱的向量快很多吗?@DiegoNolan向量更快。我说的是标量值你说的避免数据结构是什么意思?三个单独的单词参数是否比一个包含三个单词的记录(比如SymbolFreq)更好?如果我只发送一个带有[SymbolFreq]的列表进行编码,是否同样适用?[字,字,字]会不同吗?