【问题标题】:Why is the performance of Apache Spark worse than R on a local cluster?为什么 Apache Spark 在本地集群上的性能比 R 差?
【发布时间】:2015-08-27 04:25:53
【问题描述】:

我在本地机器上比较 R 和 Apache Spark 的性能,R 似乎做得更好。那是因为我没有使用集群还是我做错了什么?

创建数据 (create_data.R):

options = commandArgs(trailingOnly = TRUE)
rows = as.numeric(options[1])

perday = 365 / (rows-1) * 6
dates = seq(as.Date('2010-01-01'), as.Date('2015-12-31'), by=perday)
rows = length(dates)
ids = sample(paste0("ID", seq(1:10000)), rows, replace=TRUE)
sales = rpois(rows,50)
categories = sample(paste("Category", sprintf("%02d",seq(1:10))), rows, replace=TRUE)
data = data.frame(dates, ids, sales, categories)

write.csv(data, "/home/phil/performance/data.csv", row.names=FALSE)

测试 R (cut.R):

suppressMessages(suppressWarnings(require(dplyr, quietly=TRUE)))

data = read.csv("data.csv")
first_purchase = head(data[order(data$dates, data$ids),],1)

print(first_purchase)

测试 Spark (cut.py):

from pyspark import SparkContext

sc = SparkContext("local")

rdd = sc.textFile("data.csv", 2)

# Get rid of header
header = rdd.take(1)[0]
rdd = rdd.filter(lambda line: line != header)

rdd = rdd.map(lambda line: line.split(","))
first_purchase = rdd.takeOrdered(1, lambda x: [x[0],x[1]])[0]
print(first_purchase)

运行完整测试 (run_tests.sh):

echo "Creating data"
Rscript create_data.R 5000000
wc -l data.csv

echo "Testing R"
time Rscript cut.R

echo "Testing Spark"
time spark-submit cut.py

测试输出:

$ . run_test.sh
Creating data
5000001 data.csv
Testing R
          dates  ids sales  categories
1264 2010-01-01 ID10    60 Category 01

real    0m12.689s
user    0m12.498s
sys     0m0.187s
Testing Spark
[u'2010-01-01', u'"ID10"', u'60', u'"Category 01"']

real    0m17.029s
user    0m7.388s
sys     0m0.392s

我在一个 Ubuntu 上以 Windows 7 作为主机系统的 VirtualBox 运行它,如果这会有所不同的话。

【问题讨论】:

    标签: r performance sorting apache-spark


    【解决方案1】:

    Spark 是一个分布式计算框架,它的模型是将工作分解为多个部分(任务),其中这些任务是根据从 RDD 上定义的功能转换中的依赖关系派生的 DAG 调度、序列化和交付的。

    所有这些机器都会产生间接费用,即使在本地模式下也是如此。与 R 相比,为单节点执行而设计的 R 运行速度更快也就不足为奇了。

    在集群上尝试相同的比较...哦...等等... R 仅在单个节点中运行(but not for long anymore)

    【讨论】:

    • 但是 Spark 不应该仍然并行化操作吗?由于 R 没有并行化,我预计 Spark 会有所改进。但也许排序只是作为测试运行的错误工作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多