Last active
October 31, 2022 08:27
-
-
Save Gsantomaggio/921d2de9d1c751f6b3b4bfecca4e6a9d to your computer and use it in GitHub Desktop.
This file contains 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
import json | |
import time | |
from json import JSONEncoder | |
class Foo: | |
def __init__(self, string_1, string_2, int_1): | |
self.string_1 = string_1 | |
self.string_1 = string_2 | |
self.int_1 = int_1 | |
class FooWithNested: | |
def __init__(self, int64_2, int64_1, foo_nested_): | |
self.int64_2 = int64_2 | |
self.int64_1 = int64_1 | |
self.foo_nested = foo_nested_ | |
# subclass JSONEncoder | |
class FooEncoder(JSONEncoder): | |
def default(self, o): | |
return o.__dict__ | |
start_time = time.time() | |
x = 1_000_000 | |
for n in range(x): | |
foo = Foo("String_1", "string_2", 123) | |
foo_nested = FooWithNested(n + 100, n, foo) | |
json_string = FooEncoder().encode(foo_nested) | |
obj = json.loads(json_string, cls=json.JSONDecoder) | |
txt2 = "Seconds: {0}, iterations: {1}".format((time.time() - start_time), x) | |
print(txt2) |
Author
Gsantomaggio
commented
Oct 31, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment