是和在python中

Speed up your string comparisons

加快字符串比较

Many programmers and beginners love python. Python is one of the languages that is witnessing incredible growth and popularity year by year.

许多程序员和初学者都喜欢python。 Python是一种每年都见证着惊人的增长和普及的语言。

In 2017, Stack overflow calculated that python would beat all other programming languages by 2020 as it has become the fastest-growing programming language in the world.Python language is one of the most accessible programming languages available because of its Readability, tons of libraries ,Vibrant Community , can be used for Web Programming, Desktop Applications, Big data, Cloud Computing, Machine Learning and so on.

在2017年, 堆栈溢出 据计算,到2020年python将成为所有增长最快的编程语言,届时python将击败所有其他编程语言。 Python语言因其易读性,大量的库,充满活力的社区而成为最易访问的编程语言之一,可用于Web编程,桌面应用程序,大数据,云计算,机器学习等。

It’s very important for every Pythonista to understand the difference between == (equality operator ) and is ( identity operator ) before you use. But, many of us are confused about which one to use in which situation. Let’s understand it clearly.

对于每个Pythonista来说,在使用前了解== (相等运算符)和is (身份运算符)之间的区别非常重要。 但是,我们很多人对于在哪种情况下使用哪个感到困惑。 让我们清楚地了解它。

In this article, I will discuss

在本文中,我将讨论

  1. What is equality and identity operators

    什么是平等和身份算子
  2. When to use equality and identity operators

    何时使用相等和标识运算符
  3. Which one is best.

    哪一个最好。

1.什么是==,是运算符 (1. What is == and is operators)

Image for post
When var1 & var2 variables are created, Memory will be allocated as above
创建var1和var2变量后,将按上述方式分配内存

The ‘is’ operator may seem like the same as the == (equality operator) but they are not same. The ‘is’ is an identity operator used to checks whether two variables point to the same object in memory or to compare identity of two objects . It returns True if the operands are identical (refer to the same object).

'is'运算符可能看起来与== (相等运算符)相同,但它们并不相同。 “ is”是一个身份运算符,用于检查两个变量是否指向内存中同一对象比较两个对象的身份 。 如果操作数相同(返回同一对象),则返回True。

Let’s see with an example,

让我们看一个例子,

Image for post

Above code returned False because the two variables pointing to the different objects i.e. different memory addresses (0x7fce1afef330 & 0x7fce1afef050). To get the memory addresses of var1 & var2 use hex(id(var1)) and hex(id(var2)).

上面的代码返回False,因为这两个变量指向不同的对象,即不同的内存地址( 0x7fce1afef3300x7fce1afef050 )。 要获取var1和var2的内存地址,请使用hex(id(var1))hex(id(var2))。

On the other hand, the == operator checks if the values for the two variables are the same. It returns True if both operands are equal.

另一方面, ==运算符检查两个变量的是否相同。 如果两个操作数相等,则返回True。

Image for post

As expected, above code returned True because the contents of the two variables are equal.

不出所料,上面的代码返回True,因为两个变量的内容相等。

2.何时使用==和is运算符 (2. When to use == and is operators)

  1. As a rule of thumb, Use the == operator when you primary objective is to compare the contents / values of two objects and you don’t care about where they’re stored in memory. Use the is operator only when you want to check whether two variables are pointing to the same memory address.

    根据经验,请使用 ==运算符,当您的主要目标是比较两个对象的内容/值,而不必关心它们在内存中的存储位置。 使用 仅当您要检查两个变量是否指向相同的内存地址时is运算符。

  2. When you perform string comparison operation with ‘==’ operator, python compares character by character.

    当您使用'=='运算符执行字符串比较操作时,python会逐个字符地进行比较。

Image for post

The string(‘Abc’) used in the above example is very small. But what if we have a long string ? Python still compares character by character. Won’t it be a slow process ? Instead of comparing content, we can compare their memory addresses which are just integers. So, comparing integers are far better than comparing two long strings character by character. This is much faster and less CPU utilization than comparing character by character.

上例中使用的字符串('Abc')非常小。 但是,如果我们的弦长了怎么办? Python仍然逐字符进行比较。 这不是一个缓慢的过程吗? 除了比较内容外,我们还可以比较它们的内存地址,这些地址只是整数。 因此,比较整数远胜于逐个字符比较两个长字符串。 与逐字符进行比较相比,这要快得多CPU使用率也要低得多。

NOTE : Python points variables to same memory address when they hold same content. Not all strings are automatically interned in python. But, we can force strings to be interned by using the sys.intern() .

注意:当Python包含相同内容时,它们会将变量指向相同的内存地址。 并非所有字符串都自动插入python。 但是,我们可以使用sys.intern()强制字符串被嵌入。

So, Use ‘is’ (identity) operator than ‘==’ (equality) operator to speed up your heavy string comparison operations.

因此,使用'is'(身份)运算符而不是'=='(相等)运算符可以加快繁重的字符串比较操作。

3.哪一个最好 (3. Which one is best)

This is definitely individual’s choice, but if you talk about speed ‘is’ (identity) beats ‘==’ (equality) operator.

这绝对是个人的选择,但是如果您谈论速度'is' ( 身份)胜过'=='(平等)运算符。

Let’s check with code which one is actually best in terms of speed.

让我们检查一下在速度方面实际上哪个最好的代码。

== operator execution time :

==操作员执行时间:

import timedef equality_operator(n):
a = ‘Which one is best? identity or equality ?’ * 1000
b = ‘Which one is best? identity or equality ?’ * 1000
for i in range(n):
if a == b :
passstart = time.perf_counter()
equality_operator(10000000)
end = time.perf_counter()
print('Total time :',end - start)-------------------------------------
Total time 14.075116038999113

is operator execution time :

操作员执行时间:

import sysdef identity_operator(n):
a = sys.intern(‘Which one is best? identity or equality ?’ * 1000)
b = sys.intern(‘Which one is best? identity or equality ?’ * 1000)
for i in range(n):
if a is b :
pass
# print(True)start = time.perf_counter()
identity_operator(10000000)
end = time.perf_counter()
print(‘Total time :’,end — start)-----------------------------------------
Total time : 0.3351166579996061

Above results shows ‘is’ beats ‘==’ operator in execution time.

以上结果显示,执行时间是'is'胜过'=='运算符。

感谢您的阅读! (Thank you for reading!)

Any feedback and comments are, greatly appreciated!

任何反馈和评论都非常感谢!

翻译自: https://medium.com/swlh/is-and-in-python-f084f36cbc0e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值