【问题标题】:Pandas UDF no faster than Spark UDF? [duplicate]Pandas UDF 不比 Spark UDF 快? [复制]
【发布时间】:2020-08-28 19:07:48
【问题描述】:

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

谢谢!

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

【问题讨论】:

  • pandas_udf 针对分组操作进行了优化并且速度更快,例如在 groupBy 之后应用 pandas_udf。分组允许 pandas 执行矢量化操作,并且会比普通的 udf 更快。对于像 a*b 这样的正常情况,正常的 spark udf 就足够了,而且速度更快。

标签: apache-spark pyspark


【解决方案1】:

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

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

因此,如果您在小数据集上比较性能,pandas 的性能可以胜过 spark。

【讨论】:

  • 谢谢 - 我在一个小数据集上进行测试,因为我拥有的实时数据是 6PB,我不想把它用作我的游乐场。但!也许我需要!
  • 对于 6 PB,您肯定必须选择 spark 并执行大量集群优化和代码优化。
  • 确保你已经配置了多个worker。如果你在本地机器上运行它,请确保你有足够的 CPU。是的,这应该是显而易见的,但我无法告诉你有多少“工作”的多线程代码一旦有额外的处理器线程可用就会失败。
猜你喜欢
  • 2018-11-22
  • 1970-01-01
  • 2018-07-16
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
  • 2021-04-03
  • 2016-02-13
  • 2017-08-13
相关资源
最近更新 更多