Python Observable Group By非常慢

时间:2019-02-10 01:53:20

标签: python reactive-programming

我正在尝试使用rx包在Python中进行反应式编程,发现group_by操作与常规方法相比非常慢(几乎是100倍)。我使用的测试代码如下。

import datetime
from collections import defaultdict
from rx import Observable

def key_selector(x):
    if x % 2 == 0:
        return 'even'
    return 'odd'

def subscribe_group_observable(group_observable):
    def print_count(count):
        print('Group observable key {} contains {} items'.format(group_observable.key, count))
    group_observable.count().subscribe(print_count)

def show_time(fn):
    def new_fn(*args, **kwargs):
        print('running ' + fn.__name__)
        start_time = datetime.datetime.now()
        result = fn(*args, **kwargs)
        end_time = datetime.datetime.now()
        print('Elapsed = {}'.format((end_time - start_time).total_seconds()))
        return result
    return new_fn

@show_time
def test_groupby_rx(src):
    groups = Observable.from_(src).group_by(key_selector)
    groups.subscribe(subscribe_group_observable)

@show_time
def test_groupby_regular(src):
    results = defaultdict(int)
    for val in src:
        results[key_selector(val)] += 1
    for key in results:
        print('Group observable key {} contains {} items'.format(key, results[key]))

@show_time
def test_groupby_regular_2(src):
    results = defaultdict(int)
    results["even"] = len([x for x in src if x % 2 == 0])
    results["odd"] = len([x for x in src if x % 2 == 1])
    for key in results:
        print('Group observable key {} contains {} items'.format(key, results[key]))

if __name__ == '__main__':
    src = range(100_000)
    test_groupby_rx(src)
    test_groupby_regular(src)
    test_groupby_regular_2(src)
    print('Done')

输出如下:

running test_groupby_rx
Group observable key even contains 50000 items
Group observable key odd contains 50000 items
Elapsed = 2.21577
running test_groupby_regular
Group observable key even contains 50000 items
Group observable key odd contains 50000 items
Elapsed = 0.02
running test_groupby_regular_2
Group observable key even contains 50000 items
Group observable key odd contains 50000 items
Elapsed = 0.012
Done

有没有办法在Python RX中获得可比的性能?我在Windows上使用的是Python 3.7.0。

0 个答案:

没有答案