Merge pull request #5776 from SimonEbner/performance_decimalspace

Improve performance of decimalspace.py
This commit is contained in:
Matthias 2021-10-24 19:27:24 +02:00 committed by GitHub
commit 4539170424
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,11 +7,15 @@ class SKDecimal(Integer):
def __init__(self, low, high, decimals=3, prior="uniform", base=10, transform=None, def __init__(self, low, high, decimals=3, prior="uniform", base=10, transform=None,
name=None, dtype=np.int64): name=None, dtype=np.int64):
self.decimals = decimals self.decimals = decimals
_low = int(low * pow(10, self.decimals))
_high = int(high * pow(10, self.decimals)) self.pow_dot_one = pow(0.1, self.decimals)
self.pow_ten = pow(10, self.decimals)
_low = int(low * self.pow_ten)
_high = int(high * self.pow_ten)
# trunc to precision to avoid points out of space # trunc to precision to avoid points out of space
self.low_orig = round(_low * pow(0.1, self.decimals), self.decimals) self.low_orig = round(_low * self.pow_dot_one, self.decimals)
self.high_orig = round(_high * pow(0.1, self.decimals), self.decimals) self.high_orig = round(_high * self.pow_dot_one, self.decimals)
super().__init__(_low, _high, prior, base, transform, name, dtype) super().__init__(_low, _high, prior, base, transform, name, dtype)
@ -25,9 +29,9 @@ class SKDecimal(Integer):
return self.low_orig <= point <= self.high_orig return self.low_orig <= point <= self.high_orig
def transform(self, Xt): def transform(self, Xt):
aa = [int(x * pow(10, self.decimals)) for x in Xt] return super().transform([int(v * self.pow_ten) for v in Xt])
return super().transform(aa)
def inverse_transform(self, Xt): def inverse_transform(self, Xt):
res = super().inverse_transform(Xt) res = super().inverse_transform(Xt)
return [round(x * pow(0.1, self.decimals), self.decimals) for x in res] # equivalent to [round(x * pow(0.1, self.decimals), self.decimals) for x in res]
return [int(v) / self.pow_ten for v in res]