Quantcast
Viewing all articles
Browse latest Browse all 2067

Go 1.20 :: ปรับปรุงการแปลงค่าจาก array ไปเป็น string

Image may be NSFW.
Clik here to view.

Image may be NSFW.
Clik here to view.

เช้านี้เห็นในกลุ่ม Go กำลังพูดคุยเกี่ยวกับ Go 1.20 ว่า
ในการแปลงค่าจาก array ไปเป็น string นั้นเร็วขึ้นอย่างมาก
และลดการจองพื้นที่ในหน่วยความจำลงไปอย่างมาก
โดยใช้ function ชื่อว่า String() ใน package unsafe
มาดูตัวอย่างการใช้งานกัน

อย่างแรกทำการ update Go มาใช้ Go 1.20 rc1 ก่อน

[gist id="bc1250bbfe88d0f3d673acec16bfeb4f" file="1.txt"]

จากนั้นทำการเขียน code ในรูปแบบเก่าใช้ string() ปกติ
และใช้ String() จาก package unsafe ดังนี้

[gist id="bc1250bbfe88d0f3d673acec16bfeb4f" file="demo.go"]

เขียนชุดการทดสอบ เพื่อ benchmark ผลการทำงาน

[gist id="bc1250bbfe88d0f3d673acec16bfeb4f" file="demo_test.go"]

ทำการ benchmark ดู ผลท่ีได้เป็นดังนี้

[gist id="bc1250bbfe88d0f3d673acec16bfeb4f" file="2.txt"]

ได้ผลเร็วขึ้นอย่างมาก น่าใช้มาก ๆ
และสามารถไปดูความสามารถอื่น ๆ ได้ที่ Release note 1.20


Viewing all articles
Browse latest Browse all 2067

Trending Articles