Skip to content

Instantly share code, notes, and snippets.

@amos402
Last active November 6, 2020 18:13
Show Gist options
  • Save amos402/8decd54dee57fe654e098bc0b15c061a to your computer and use it in GitHub Desktop.
Save amos402/8decd54dee57fe654e098bc0b15c061a to your computer and use it in GitHub Desktop.
import functools
import os
import sys
import timeit
sys.path.append(os.path.join(os.path.dirname(__file__), r"../CSLibrary/bin/Release"))
import clr
clr.AddReference("CSLibrary")
from CSLibrary import TestObject
class ObjTest:
def __init__(self):
self._tests = []
self._obj = TestObject()
def __call__(self, func):
def run():
res = timeit.repeat(call)
print(func.__name__, round(min(res), 5), "seconds")
call = functools.partial(func, self._obj)
self._tests.append((call, run))
def exec(self):
for func, test in self._tests:
func() # pre run once
test()
obj_test = ObjTest()
method = obj_test._obj.VoidCall_1
@obj_test
def get_doc(obj):
method.__doc__
@obj_test
def get_attr_doc(obj):
getattr(method, "__doc__", None)
doc = "__doc_"
_ = 1
doc = doc + "_"
assert id(doc) != id("__doc__")
@obj_test
def get_attr_doc_concat(obj):
getattr(method, doc, None)
def main():
os.system("wmic cpu get name")
obj_test.exec()
print("done")
if __name__ == "__main__":
sys.exit(int(main() or 0))
import io
import sys
def read_result(path):
d = {}
with open(path, encoding="utf-16-le") as f:
for line in f.readlines():
line = line.strip()
if not line.endswith("seconds"):
continue
func, span, _ = line.split(" ")
d[func] = float(span)
return d
def main():
lst = [
read_result("origin.txt"),
read_result("intern-string.txt"),
read_result("intern-string-no-cache.txt")
]
print("| function | origin | intern-string | without Intern.GetManagedString |")
print("| ---- | ---- | ---- | ---- |")
for func in lst[0].keys():
sb = io.StringIO()
sb.write(f"| {func} |")
for i, result in enumerate(lst):
span = result[func]
if i == 0:
standard = span
percent = "{:.2%}%".format(span / standard)
sb.write(f" {span} ({percent}) |")
print(sb.getvalue())
if __name__ == "__main__":
sys.exit(int(main() or 0))
namespace CSLibrary
{
public class TestObject
{
public void VoidCall_1(int x)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment