1

I'm trying to reduce the execution time of the program by replacing the "input" with "sys.stdin.readline". But something doesnt wrong.

The first line contains text consisting of words separated by single spaces. The next line of input is given a number - the number of encrypted words. The next n lines contain encrypted words. Lines are displayed that contain the decryption (breaking Caesar's cipher).

Code with "input":

import sys

letters = 'abcdefghijklmnopqrstuvwxyz'
inp_str = input('')     # 'a abb bab abc'
inp_n = int(input())    # 3
inp_str_list = inp_str.split(' ')
list_secret_words = [input() for x in range(inp_n)]     # 'def' 'abc' 'aza'

for i in list_secret_words:
    message = i
    for key in range(len(letters)):
        translated = ''
        for symbol in message:
            if symbol in letters:
                num = letters.find(symbol)
                num = num - key
                if num < 0:
                    num = num + len(letters)
                translated = translated + letters[num]
            else:
                translated = translated + symbol
        for x in inp_str_list:
            if translated == x:
                sys.stdout.write(translated+'\n')  # result: "abc" "abc" "bab" (right)

Code with "sys.stdin.readline":

import sys

letters = 'abcdefghijklmnopqrstuvwxyz'
inp_str = sys.stdin.readline()    # 'a abb bab abc'
inp_n = int(sys.stdin.readline())    # 3
inp_str_list = inp_str.split(' ')
list_secret_words = [sys.stdin.readline() for x in range(inp_n)]     # 'def' 'abc' 'aza'

for i in list_secret_words:
    message = i
    for key in range(len(letters)):
        translated = ''
        for symbol in message:
            if symbol in letters:
                num = letters.find(symbol)
                num = num - key
                if num < 0:
                    num = num + len(letters)
                translated = translated + letters[num]
            else:
                translated = translated + symbol
        for x in inp_str_list:
            if translated == x:
                sys.stdout.write(translated+'\n')  # result: "abc" " " "abc" (wrong)

  • "But something doesnt wrong" - care to elaborate? – rdas Mar 28 '20 at 23:27
  • @rdas I think maybe problem with `inp_str_list = inp_str.split(' ')`: a abb bab abc —»['a', 'abb', 'bab', 'abc\n']. – Nikita Biryukov Mar 28 '20 at 23:31
  • Is there a reason you expect this to significantly reduce execution time? It's generally good habit to profile your software and determine where its time is actually being spent before attempting microoptimizations. – Charles Duffy Mar 29 '20 at 06:10
  • And yes, you want to strip newlines, probably before splitting. – Charles Duffy Mar 29 '20 at 06:11
  • @CharlesDuffy Yes, in this solution `input()` is slower than `sys.stdin.readline()`. – Nikita Biryukov Mar 29 '20 at 07:26
  • 1
    I'm not, here, questioning your assertion that it's slower; I'm questioning the assertion that the difference constitutes some worthwhile percentage of your overall runtime -- unless you've run measurements to that effect. Some of the later string handling is pretty grotty; at a read through, that's the place with more obvious room for optimization. – Charles Duffy Mar 29 '20 at 13:37
  • @CharlesDuffy Thank you for understanding. I just need to reduce the code execution time by 0.074s. And the use of sys.stdin - should have helped. Yes, I don’t deny it, nested loops also complicate the code, but I decided to deal with sus.stdin. – Nikita Biryukov Mar 29 '20 at 17:33
  • Anyhow -- general practice is to use `strip()` to trim the trailing `'\n'`. Backing up: How much data is being run through for the test set in which we're trying to get `0.074s` of extra performance? – Charles Duffy Mar 29 '20 at 17:38
  • 1
    See https://ideone.com/kKBMDI emitting `abc` `abc` `bab`; it differs from your original code only by adding `.rstrip()` after each `sys.stdin.readline()`. – Charles Duffy Mar 29 '20 at 17:39
  • @CharlesDuffy Yes, thanks. – Nikita Biryukov Mar 29 '20 at 17:57

0 Answers0