Created
March 19, 2014 21:55
-
-
Save asterite/9652252 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Foo | |
| property x | |
| property y | |
| property z | |
| property w | |
| def initialize | |
| end | |
| def dup | |
| self_buf = pointerof(self) as UInt8* | |
| self_size = instance_sizeof(self) | |
| buf = Pointer(UInt8).malloc(self_size) | |
| # C.memcpy(buf as Void*, self_buf as Void*, self_size) | |
| buf.memcpy(self_buf, self_size) | |
| buf as self | |
| end | |
| def dup2 | |
| f2 = Foo.new | |
| f2.x = x | |
| f2.y = y | |
| f2.z = z | |
| f2.w = w | |
| f2 | |
| end | |
| def value | |
| x.to_i + y.to_i + z.to_i + w.to_i | |
| end | |
| end | |
| f = Foo.new | |
| f.x = 1 | |
| f.y = 2 | |
| f.z = 3 | |
| f.w = 4 | |
| time = Time.now | |
| a = 0 | |
| 20_000_000.times do | |
| g = f.dup | |
| a += g.value | |
| end | |
| puts Time.now - time | |
| time = Time.now | |
| a = 0 | |
| 20_000_000.times do | |
| g = f.dup2 | |
| a += g.value | |
| end | |
| puts Time.now - time | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment