- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在处理大数据,工作的一个重要步骤是获取数千个字符串的所有排序组合。
在 python 3.4 中,我尝试了三种方法来执行此操作。
首先,将数据伪造为字符串列表,有或没有 GO 术语:
# -*- coding: utf-8 -*-
import random as rd
# With GO term
# data = ["GO:" + str(i) for i in range(0, 10000)]
# Xor, without GO term
data = [str(i) for i in range(0, 10000)]
rd.shuffle(data)
print("shuffle done")
GO 术语只是添加到所有数据字符串开头的常量字符串。有和没有 GO 项的结果如下。
现在,执行基准测试:
from itertools import combinations, product
def test1(): # use condition
g = ((i,j) if (i < j) else (j,i) for i, j in combinations(data, 2))
print(len([(i, j) for i, j in g]))
def test2(): # use filter
g = ((i, j) for i,j in product(data, data) if (i < j))
print(len([(i, j) for i, j in g]))
def test3(): # sort before combine
g = ((i,j) for i, j in combinations(sorted(data), 2))
print(len([(i, j) for i, j in g]))
import timeit
print(timeit.timeit(stmt=test1, number=3))
print(timeit.timeit(stmt=test2, number=3))
print(timeit.timeit(stmt=test3, number=3))
使用 GO 术语的输出:
49995000
49995000
49995000
23.490827083587646
49995000
49995000
49995000
31.04393219947815
49995000
49995000
49995000
16.878661155700684
没有 GO 术语的输出:
49995000
49995000
49995000
22.99237084388733
49995000
49995000
49995000
29.025460958480835
49995000
49995000
49995000
16.596422910690308
每次调用打印的 49995000 向我们证明所有方法都产生相同数量的数据。现在的问题:
我的第一个猜测是排序+组合产生的数据比较要少得多,而其他两种方法对每对字符串进行比较,但是,由于排序听起来像是一项繁重的操作,我对此并不完全确定.
最佳答案
我很确定造成您所观察到的性能差异的重要因素是检查 if (i < j)
。 49995000 次 vs 对 10000 个元素的列表进行排序,而不是假定的已排序 vs 未排序的可迭代对象。
combinations
在这两种情况下应该做相同数量的工作,因为它们产生相同数量的元素并且不对元素进行排序并按字典顺序返回它们。
要正确测试排序是否有所不同:
对同一组数据执行相同的条件检查,但已排序和未排序:
sorted_data = sorted(data)
def test1():
g = ((i,j) if (i < j) else (j,i) for i, j in combinations(sorted_data, 2))
return len([(i, j) for i, j in g])
def test2():
g = ((i,j) if (i < j) else (j,i) for i, j in combinations(data, 2))
return len([(i, j) for i, j in g])
%timeit test1()
1 loops, best of 3: 23.5 s per loop
%timeit test2()
1 loops, best of 3: 24.6 s per loop
在没有条件的情况下执行测试:
def test3():
g = ((i,j) for i, j in combinations(sorted_data, 2))
return len([(i, j) for i, j in g])
def test4():
g = ((i,j) for i, j in combinations(data, 2))
return len([(i, j) for i, j in g])
%timeit test3()
1 loops, best of 3: 20.7 s per loop
%timeit test4()
1 loops, best of 3: 21.3 s per loop
Why is the first method so quick, compared to the second ? Use of filter is the main way i use for filtering data. Until now, i assume that the filter form was heavily optimized.
使用组合会产生较少的条件检查元素。 10000C2 = 49995000
对于组合与 10000**2 = 100000000
用于产品。
Why the GO term impact more the first and the second method than the third one ?
第一种和第二种方法受比较49995000次和100000000次附加字符的影响。第三个仅受对 10000 个项目进行排序所需的比较的影响。
经过一些摆弄后,似乎排序可能会有所不同,但不会像使用条件一样大。不知道是什么原因造成的。
from itertools import combinations
import random as rd
data = ["{0:04d}".format(i) for i in range(0, 10000)] # Normalize str length
rd.shuffle(data)
sorted_data = sorted(data)
reversed_sorted_data = sorted_data[::-1]
def test1():
g = list((i,j) if (i < j) else (j,i) for i, j in combinations(data, 2))
print('unsorted with conditional: ', len(g))
%timeit test1()
# unsorted with conditional: 49995000
# unsorted with conditional: 49995000
# unsorted with conditional: 49995000
# unsorted with conditional: 49995000
# 1 loops, best of 3: 20.7 s per loop
def test2():
g = list((i,j) if (i < j) else (j,i) for i, j in combinations(sorted_data, 2))
print('sorted with conditional: ', len(g))
%timeit test2()
# sorted with conditional: 49995000
# sorted with conditional: 49995000
# sorted with conditional: 49995000
# sorted with conditional: 49995000
# 1 loops, best of 3: 19.6 s per loop
def test3():
g = list((i,j) for i, j in combinations(data, 2))
print('unsorted without conditional: ', len(g))
%timeit test3()
# unsorted without conditional: 49995000
# unsorted without conditional: 49995000
# unsorted without conditional: 49995000
# unsorted without conditional: 49995000
# 1 loops, best of 3: 15.7 s per loop
def test4():
g = list((i,j) for i, j in combinations(sorted_data, 2))
print('sorted without conditional: ', len(g))
%timeit test4()
# sorted without conditional: 49995000
# sorted without conditional: 49995000
# sorted without conditional: 49995000
# sorted without conditional: 49995000
# 1 loops, best of 3: 15.3 s per loop
def test5():
g = list((i,j) for i, j in combinations(reversed_sorted_data, 2))
print('reverse sorted without conditional: ', len(g))
%timeit test5()
# reverse sorted without conditional: 49995000
# reverse sorted without conditional: 49995000
# reverse sorted without conditional: 49995000
# reverse sorted without conditional: 49995000
# 1 loops, best of 3: 15 s per loop
关于python - 预排序提高了 itertools.combinations 的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30434798/
在这段令人惊叹的视频 ( https://www.youtube.com/watch?v=udix3GZouik ) 中,Alex Blom 谈到了 Ember 在移动世界中的“黑客攻击”。 在 22
我们希望通过我们的应用收集使用情况统计信息。因此,我们希望在服务器端的某个地方跟踪用户操作。 就性能而言,哪个选项更合适: 在 App Engine 请求日志中跟踪用户操作。即为每个用户操作写入一个日
在针对对象集合的 LINQ 查询的幕后究竟发生了什么?它只是语法糖还是发生了其他事情使其更有效的查询? 最佳答案 您是指查询表达式,还是查询在幕后的作用? 查询表达式首先扩展为“普通”C#。例如: v
我正在构建一个简单的照片库应用程序,它在列表框中显示图像。 xaml 是:
对于基于 Web 的企业应用程序,使用“静态 Hashmap 存储对象” 和 apache java 缓存系统有何优缺点?哪一个最有利于性能并减少堆内存问题 例如: Map store=Applica
我想知道在性能方面存储类变量的最佳方式是什么。我的意思是,由于 Children() 函数,存储一个 div id 比查找所有其他类名更好。还是把类名写在变量里比较好? 例如这样: var $inne
我已经阅读了所有这些关于 cassandra 有多快的文章,例如单行读取可能需要大约 5 毫秒。 到目前为止,我不太关心我的网站速度,但是随着网站变得越来越大,一些页面开始需要相当多的查询,例如一个页
最近,我在缓存到内存缓存之前的查询一直需要很长时间才能处理!在这个例子中,它花费了 10 秒。在这种情况下,我要做的就是获得 10 个最近的点击。 我感觉它加载了所有 125,592 行然后只返回 1
我找了几篇文章(包括SA中的一些问题),试图找到基本操作的成本。 但是,我尝试制作自己的小程序,以便自己进行测试。在尝试测试加法和减法时,我遇到了一些问题,我用简单的代码向您展示了这一点
这个问题在这里已经有了答案: Will Java app slow down by presence of -Xdebug or only when stepping through code? (
我记得很久以前读过 with() 对 JavaScript 有一些严重的性能影响,因为它可能对范围堆栈进行非确定性更改。我很难找到最近对此的讨论。这仍然是真的吗? 最佳答案 与其说 with 对性能有
我们有一个数据仓库,其中包含非规范化表,行数从 50 万行到 6 多万行不等。我正在开发一个报告解决方案,因此出于性能原因我们正在使用数据库分页。我们的报告有搜索条件,并且我们已经创建了必要的索引,但
我有一条有效的 SQL 语句,但需要很长时间才能处理 我有一个 a_log 表和一个 people 表。我需要在 people 表中找到给定人员的每个 ID 的最后一个事件和关联的用户。 SELECT
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
通常当我建立一个站点时,我将所有的 CSS 放在一个文件中,并且一次性定义与一组元素相关的所有属性。像这样: #myElement { color: #fff; background-
两者之间是否存在任何性能差异: p { margin:0px; padding:0px; } 并省略最后的分号: p { margin:0px; padding:0px } 提前致谢!
我的应用程序 (PHP) 需要执行大量高精度数学运算(甚至可能出现一共100个数字) 通过这个论坛的最后几篇帖子,我发现我必须使用任何高精度库,如 BC Math 或 GMP,因为 float 类型不
我一直在使用 javamail 从 IMAP 服务器(目前是 GMail)检索邮件。 Javamail 非常快速地从服务器检索特定文件夹中的消息列表(仅 id),但是当我实际获取消息(仅包含甚至不包含
我非常渴望开发我的第一个 Ruby 应用程序,因为我的公司终于在内部批准了它的使用。 在我读到的关于 Ruby v1.8 之前的所有内容中,从来没有任何关于性能的正面评价,但我没有发现关于 1.9 版
我是 Redis 的新手,我有一个包含数百万个成员(member) ID、电子邮件和用户名的数据集,并且正在考虑将它们存储在例如列表结构中。我认为 list 和 sorted set 可能最适合我的情
我是一名优秀的程序员,十分优秀!