简体   繁体   English

性能if(typeof x =='number')?

[英]Performance if (typeof x == 'number')?

I've been Googling quite some time for eg 'typeof' and 'performance', but I haven't been able to find a satisfactory answer to the following problem. 我一直在谷歌搜索一段时间,例如'typeof'和'performance',但我还没有找到一个满意的答案来解决以下问题。

I am trying to implement complex numbers for the Transcrypt Python to JavaScript compiler, using local operator overloading. 我正在尝试使用本地运算符重载为Transcrypt Python到JavaScript编译器实现复数。 Since we're dealing with a dynamically typed language, it cannot be predicted what type of data will be in a variable. 由于我们正在处理动态类型语言,因此无法预测变量中的数据类型。

If I translate x + y to JavaScript, having operator overloading switched on, it will translate eg as __add__ (x, y) In order to do the right thing, the __add__ function has to check for both x and y whether they are 'ordinary' JavaScript numbers or if one of them or both of them are of type 'complex', since that requires special operations. 如果我将x + y转换为JavaScript,打开运算符重载,它将转换为例如__add__ (x, y)为了做正确的事情, __add__函数必须检查xy是否是'普通的'JavaScript编号或者其中一个或两个都是'复杂'类型,因为这需要特殊操作。

The most obvious way to do that is to test for typeof x == 'number' . 最明显的方法是测试typeof x == 'number' However, coming from a C/C++ background, it seems ridiculously inefficient to test for equality with a string with six characters which on top of that first has to be retrieved from memory, only to possible add two integers, which for many processors, once parsed, would be only one instruction. 然而,来自C / C ++背景,用一个带有六个字符的字符串来测试相等性似乎是非常低效的,首先必须从内存中检索它,只有可能添加两个整数,这对于许多处理器来说,一次解析后,只会有一条指令。

What amazes me most is that checks like this are advised everywhere around the internet as the normal thing to do. 让我感到惊讶的是,像这样的检查建议在互联网的各个地方作为正常的事情。 Does anyone know if x == 'number' or possible x === 'number' is somehow cleverly optimized to prevent a full string comparison. 有没有人知道x == 'number'或可能的x === 'number'是以某种方式巧妙地优化,以防止完整的字符串比较。

To further clarify the problem, here's my current code for the __add__ operator, using the string comparison. 为了进一步澄清问题,这是我当前使用字符串比较的__add__运算符的代码。

def __add__ (self, other):
    if __typeof__ (other) == 'number':   # Translates to: if (typeof other == 'number') {
        return complex (self.real + other, self.imag)
    else:   # Other is complex
        return complex (self.real + other.real, self.imag + other.imag)

If not can anyone hint me on a quicker way to distinguish between a number and an arbitrary non-numerical object. 如果没有,任何人都可以通过更快的方式暗示我区分数字和任意非数字对象。

Thanks for the tips. 谢谢你的提示。 The source is now: 来源现在是:

def __sub__ (self, other):
    if __typeof__ (other, 'number'):
        return complex (self.real - other, self.imag)
    else:
        return complex (self.real - other.real, self.imag - other.imag)

translated by: 被某某人翻译:

elif node.func.id == '__typeof__':
    self.emit ('typeof ')
    self.visit (node.args [0])
    self.emit (' === ') # Give JavaScript string interning a chance to avoid char by char comparison
    self.visit (node.args [1])
    return

to: 至:

get __add__ () {return __get__ (this, function (self, other) {
    if (typeof other === 'number') {
        return complex (self.real + other, self.imag);
    }
    else {
        return complex (self.real + other.real, self.imag + other.imag);
    }
});},

That depends on the JavaScript engine. 这取决于JavaScript引擎。 But a typeof obj can only return a fixed set of strings. 但是一个类型的typeof obj只能返回一组固定的字符串。 So a compiler/engine can optimize a typeof obj === 'number' into a test that does not do a string comparison, but uses a more efficient test. 因此,编译器/引擎可以将类型的typeof obj === 'number'优化为不进行字符串比较的测试,但使用更有效的测试。

The byte code V8 created for if( typeof obj === 'number' ) will be something like this: if( typeof obj === 'number' )创建的字节代码V8将是这样的:

268 S> 0x24110cfe4b0 @   62 : 13 04     LdaImmutableCurrentContextSlot [4]
       0x24110cfe4b2 @   64 : 65 00     TestTypeOf #0
       0x24110cfe4b4 @   66 : 86 16     JumpIfFalse [22] (0x24110cfe4ca @ 88)

So at least v8 does in fact have an own command to test if an object is of a certain type, which is not a string comparison. 所以至少v8实际上有一个自己的命令来测试对象是否属于某种类型,这不是字符串比较。

I don't know if this is true for the other engines, but it is likely that they do the same thing. 我不知道其他引擎是否属于这种情况,但它们可能会做同样的事情。

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

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