Quick actions

cmd+k|ctrl+k

Navigation

Languages

6-12-2024 pm

Snippet info

Language

Python

Visibility

public

Author

chausage

Created

2024-12-06T06:28:08.219319Z

Updated

2024-12-06T07:01:34.531367Z

#case 1 string 
a='hello'
b='hello'
print(id(a))
print(id(b))
#case 2 string doesn't look like identifier
a='hello, world!'
b='hello, world!'
print(id(a))
print(id(b))
#case 3 long string but look like identifier
a='hello_world'
b='hello_world'
print(id(a))
print(id(b))
#case 4 very long
a= 'Looking to dive into the world of programming or sharpen your Python skills? Our Master Python: Complete Beginner to Advanced Course is your ultimate guide to becoming proficient in Python. This course covers everything you need to build a solid foundation from fundamental programming concepts to advanced techniques. With hands-on projects, real-world examples, and expert guidance, youll gain the confidence to tackle complex coding challenges. Whether youre starting from scratch or aiming to enhance your skills, this course is the perfect fit. Enroll now and master Python, the language of the future!'
b='Looking to dive into the world of programming or sharpen your Python skills? Our Master Python: Complete Beginner to Advanced Course is your ultimate guide to becoming proficient in Python. This course covers everything you need to build a solid foundation from fundamental programming concepts to advanced techniques. With hands-on projects, real-world examples, and expert guidance, youll gain the confidence to tackle complex coding challenges. Whether youre starting from scratch or aiming to enhance your skills, this course is the perfect fit. Enroll now and master Python, the language of the future!'
print(id(a))
print(id(b))
print('a==b',a==b)
print('a is b:', a is b)
#case 9 forces intern a, b must be set intern
import sys
a = sys.intern('hello, world!')
b=sys.intern('hello, world!')
c='hello, world!'
print(id(a))
print(id(b))
print(id(c))
#case 10 compare speed
def compare_using_equals(n):
    a='a long string that is not interned'*200
    b='a long string that is not interned'*200
    for i in range(n):
        if a==b:
            pass
def compare_using_interning(n):
    a=sys.intern('a long string that is not interned'*200)
    b=sys.intern('a long string that is not interned'*200)
    for i in range(n):
        if a is b:
            pass
        
import time

start = time.perf_counter()
compare_using_equals(10000000)
end=time.perf_counter()

print('equality:',end-start)

start = time.perf_counter()
compare_using_equals(10000000)
end=time.perf_counter()

print('identity:',end-start)





INFO