在这种特定情况下,更快的IO会导致结果变慢 [英] Faster IO giving slower results in this specific situation

查看:92
本文介绍了在这种特定情况下,更快的IO会导致结果变慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了快速读取整数,我一直在使用

cases = int(next(sys.stdin))

而不是较慢

cases = int(sys.stdin.readline())

在使用此方法解决的每个问题中,我都获得了更好的时机,但是在此SPOJ问题中相同的codechef问题我使用第二个而不是第一个获得了更好的结果.我不知道为什么会这样.

针对此特定问题,第二种方法比第一种方法突然变得更快的原因是什么?

以下是 SPOJ的计时结果解决方案

您在SPOJ计时上没有足够的数据. Codechef的时机看起来都太近了,无法打电话给我.

one .readline()调用和 one next()调用之间的任何计时差异都将归因于交换可用性,计算机上运行的其他进程, I/O刷新等.换句话说,不是这两种方法之间的任何实际性能差异.

您必须在自己的计算机上针对数千个运行进行自己的计时,才能弄清楚其中一个实际上比另一个要快.

To read in integers fastly I have been using

cases = int(next(sys.stdin))

instead of the slower

cases = int(sys.stdin.readline())

I have been getting better timing in every problem I used this but in case of this SPOJ problem and the same codechef problem I got better results using the second instead of first. I don't have any idea why this happened.

What is the reason that the 2nd method suddenly became faster compared to the 1st method for this specific problem?

Here are the Timing results of SPOJ and timing results of codechef

The 2 complete code that I used are

import sys
from itertools import islice
def p():
    cases = int(next(sys.stdin))
    string = [i[:len(i) -1] for i in islice(sys.stdin, cases)]
    for i in string:
        ops, ans = [], []
        for c in i:
            if c in ['+', '-', '*', '/', '^']:
                ops.append(c)
            elif c == ')':
                ans.append(ops.pop())
            elif c == '(':
                pass
            else:
                ans.append(c)

        print ''.join(ans)

p()

and the second one is

import sys
from itertools import islice
def p():

    cases = int(sys.stdin.readline())
    string = [i[:len(i) -1] for i in islice(sys.stdin, cases)]
    for i in string:
        ops, ans = [], []
        for c in i:
            if c in ['+', '-', '*', '/', '^']:
                ops.append(c)
            elif c == ')':
                ans.append(ops.pop())
            elif c == '(':
                pass
            else:
                ans.append(c)

        print ''.join(ans)

p()

解决方案

You do not have enough data on the SPOJ timings. The codechef timings all look too close to call to me.

Any timing difference between one .readline() call and one next() call are going to come down to differences in swap availability, other processes running on the machine, I/O flushes, etc. In other words, not to any actual performance differences between the two approaches.

You'll have to do your own timings, on your own machine, against thousands of runs to be able to figure out if one is actually faster than the other.

这篇关于在这种特定情况下,更快的IO会导致结果变慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆