1
1

More than 3 years have passed since last update.

int(num)とmath.trunc(num)とnp.trunc(num)

Last updated at Posted at 2019-11-01

bresenhamを使いたい

座標値がまとまったものをbresenham.bresenhamに突っ込みたかったがbresenhamはint型しか受け付けていなかった。

truncateは0方向に丸める関数

検証

計測はGoogleColabを使用
前準備として読み込みとかをする

base.py
import math
import numpy as np

foo = [0.0001* i for i in range(1000000)]

int(num)

%%timeit
for i in foo:
    tmp = int(i)
=>10 loops, best of 3: 105 ms per loop
---
type(tmp)
=>int

math.trunc(num)

%%timeit
for i in foo:
    tmp = math.trunc(i)
=>10 loops, best of 3: 107 ms per loop
---
type(tmp)
=>int

np.trunc(num)

%%timeit
for i in foo:
    tmp = np.trunc(i)
=>1 loop, best of 3: 831 ms per loop
---
type(tmp)
=>numpy.float64

結果

int(num) ≒ math.trunc(num) >> np.trunc(num)
と考えて良さそう

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1