So, When i am using print()
/sys.stdout.write()
to print my results (I have checked all the results are correct) the program execution time is increasing to min of 100times when i was not using print.
N = Number of elements in lists(numpy
array)
C = C is an element of array
T = Number of Arrays
1≤N≤1000
1≤Ci≤10^5
1≤T≤100
I am thinking this is happening because of print function. If yes How would I solve this issue
Without Print Execution time = 0.012797699999999967
With Print Execution time = 1.1667817
This is a question From Google KickStart
def sub_one(inputlist, inputlength):
smalllist = np.array([], dtype='int32')
outputlist = np.array([], dtype='int32')
h_score = 1
for mainnum in inputlist:
if mainnum > h_score:
smalllist = np.append(smalllist, mainnum)
else:
outputlist = np.append(outputlist, mainnum)
continue
eachlist = np.array([], dtype='int32')
for num in smalllist:
if num >= h_score:
eachlist = np.append(eachlist, h_score)
if np.count_nonzero(eachlist) == h_score:
outputlist = np.append(outputlist, h_score)
smalllist = smalllist[smalllist > h_score]
h_score = 1
return outputlist
def caseprinter(thelist):
returnstring = ""
for i in thelist:
returnstring = (str(i) " ")
return returnstring
if __name__ == "__main__":
cases = int(input())
for i in range(cases):
inputlength = int(input())
inputlist = input().split(" ")
inputlist = np.array(inputlist, dtype='int32')
outputlist = sub_one(inputlist, inputlength)
print(f'Case #{i 1}: {caseprinter(outputlist)}')```
CodePudding user response:
Your
def caseprinter(thelist):
returnstring = ""
for i in thelist:
returnstring = (str(i) " ")
return returnstring
might take quadratic time. Try linear time:
def caseprinter(thelist):
return " ".join(map(str, thelist))
CodePudding user response:
It's going to be slower as you process a huge amount of print functions. You should better write your info into files.
with open("f.txt", "a") as f:
f.write(your_str)