python中map(int、input)_使用map(int,raw_input.split)

时间:2025-04-16 07:22:27

如果您使用内置功能的map,那么它可能比LC稍快:>>> strs = " ".join(str(x) for x in xrange(10**5))

>>> %timeit [int(x) for x in ()]

1 loops, best of 3: 111 ms per loop

>>> %timeit map(int, ())

1 loops, best of 3: 105 ms per loop

使用用户定义的函数:>>> def func(x):

... return int(x)

>>> %timeit map(func, ())

1 loops, best of 3: 129 ms per loop

>>> %timeit [func(x) for x in ()]

1 loops, best of 3: 128 ms per loop

Python 3.3.1比较:>>> strs = " ".join([str(x) for x in range(10**5)])

>>> %timeit list(map(int, ()))

10 loops, best of 3: 59 ms per loop

>>> %timeit [int(x) for x in ()]

10 loops, best of 3: 79.2 ms per loop

>>> def func(x):

return int(x)

...

>>> %timeit list(map(func, ()))

10 loops, best of 3: 94.6 ms per loop

>>> %timeit [func(x) for x in ()]

1 loops, best of 3: 92 ms per loopThe only restriction is that the "loop body" of map must be a function

call. Besides the syntactic benefit of list comprehensions, they are

often as fast or faster than equivalent use of map.

相关文章