Pandas UDF 不比 Spark UDF 快吗? [复制]

2024-02-07

我从 Pyspark 网站获取了以下 UDF,因为我试图了解是否有性能改进。我制作了很大范围的数字,但两者花费的时间几乎相同,我做错了什么?

Thanks!

import pandas as pd
from pyspark.sql.functions import col, udf
from pyspark.sql.types import LongType
import time

start = time.time()
# Declare the function and create the UDF
def multiply_func(a, b):
    return a * b

multiply = udf(multiply_func, returnType=LongType())

# The function for a pandas_udf should be able to execute with local Pandas data
x = pd.Series(list(range(1, 1000000)))
print(multiply_func(x, x))
# 0    1
# 1    4
# 2    9
# dtype: int64
end = time.time()
print(end-start)

这是 Pandas UDF

import pandas as pd
from pyspark.sql.functions import col, pandas_udf
from pyspark.sql.types import LongType
import time

start = time.time()
# Declare the function and create the UDF
def multiply_func(a, b):
    return a * b

multiply = pandas_udf(multiply_func, returnType=LongType())

# The function for a pandas_udf should be able to execute with local Pandas data
x = pd.Series(list(range(1, 1000000)))
print(multiply_func(x, x))
# 0    1
# 1    4
# 2    9
# dtype: int64

除非您的数据足够大,以至于无法仅由一个节点处理,否则不应考虑 Spark。

Pandas 在单个节点上执行所有操作,而 Spark 将数据分发到多个节点进行处理。

因此,如果您在小数据集上比较性能,则 pandas 的性能可能优于 Spark。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Pandas UDF 不比 Spark UDF 快吗? [复制] 的相关文章

随机推荐