string1
^^^^^^^^^^^^^string2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^string3
dict1 = {'name':'LSH', 'age':33, 'gender':'male'}
print("name: {}, age: {}, gender: {}".format(dict1['name'], dict1['age'], dict1['gender']))
print(f"name: {dict1['name']}, age: {dict1['age']}, gender: {dict1['gender']}")
name: LSH, age: 33, gender: male
name: LSH, age: 33, gender: male
dict1 = {'name':'LSH', 'age':33, 'gender':'male'}
print("name: {}, age: {}, gender: {}".format(dict1['name'], dict1['age'], dict1['gender']))
print("name: {name}, age: {age}, gender: {gender}".format(**dict1))
print(f"name: {dict1['name']}, age: {dict1['age']}, gender: {dict1['gender']}")
print("name: {name}, age: {age}, gender: {gender}".format(**dict1))
print("name: {}, age: {}, gender: {}".format(*dict1.values()))
name: LSH, age: 33, gender: male
name: LSH, age: 33, gender: male
name: LSH, age: 33, gender: male
name: LSH, age: 33, gender: male
import timeit
str_included = """dict1 = {'name':'LSH', 'age':33, 'gender':'male'}\n"""
test_lst = [
""""name: {}, age: {}, gender: {}".format(dict1['name'], dict1['age'], dict1['gender'])
""",
""""name: {name}, age: {age}, gender: {gender}".format(**dict1)
""",
"""f"name: {dict1['name']}, age: {dict1['age']}, gender: {dict1['gender']}"
""",
""""name: {name}, age: {age}, gender: {gender}".format(**dict1)
""",
""""name: {}, age: {}, gender: {}".format(*dict1.values())
"""
]
number = 1000000
for i, t in enumerate(test_lst):
print(f"code {i:2d}: {t.strip()}")
print(f"==> {timeit.timeit(str_included + t, number=number)}")
print("="*30)
code 0: "name: {}, age: {}, gender: {}".format(dict1['name'], dict1['age'], dict1['gender'])
==> 0.850996766006574
==============================
code 1: "name: {name}, age: {age}, gender: {gender}".format(**dict1)
==> 1.0743701289757155
==============================
code 2: f"name: {dict1['name']}, age: {dict1['age']}, gender: {dict1['gender']}"
==> 0.47520723997149616
==============================
code 3: "name: {name}, age: {age}, gender: {gender}".format(**dict1)
==> 1.004713925998658
==============================
code 4: "name: {}, age: {}, gender: {}".format(*dict1.values())
==> 0.9585023220279254
==============================
댓글남기기