I want to measure the speed of the primary function of my code, and I wrote the following code:
with open(filename + '.speed.csv', 'w') as f:
f.write('result\ttime\ttext\n')
for index, text in enumerate(textlines):
#print(index)
# 1. Start to time
start = time.time()
doc = bnlu.nlps(text, enable = enable)
tags = defaultdict(set)
if 'tag' in doc:
for tag_id, tag_info_list in doc['wb_tag'].items():
for tag_info in tag_info_list:
id = tag_id
name = tag_info[1]
weight = tag_info[2]
pattern_str = tag_info[4]
tag_str = '(' + id + ' ' + name + ' ' + pattern_str + ' ' + str(weight) + ')'
tags[id].add(tag_str)
# 2. End to time
end = time.time()
time_1 = str(end-start)
f.write(str(tags) + '\t' + time_1 + '\t' + text + '\n')
# End to time 2
end2 = time.time()
time2 = end2-start
print(time2)
The time consuming code is this part:
doc = bnlu.nlps(text, enable = enable)
So my time_1 is to measure this part, and time_2 measures whether writing result of each line to a file takes much more time or just a little time compared with time_1.
Is this the right approach to measure code speed? In the resulting csv file, the 2nd column contains the time spent on each text line processed by the algorithm.
Also, the time_1 and time_2 are millseconds, right? In my output, these number are very small as below:
0.002065896987915039
0.002288341522216797
0.0019719600677490234
0.002459287643432617
0.0019350051879882812
0.002561807632446289
0.0022737979888916016
0.0026137828826904297
0.0020627975463867188
0.01592111587524414
0.001967191696166992
0.009980916976928711
0.007891178131103516
0.0022401809692382812
0.0035669803619384766
0.0030107498168945312
0.002779722213745117
0.0027618408203125
0.0019371509552001953
0.0025129318237304688
0.0023632049560546875
0.0022687911987304688
Does it mean only 0.00226 millisecond for the last? It looks suspicious since it is too fast. Is it second?
Use timeit https://docs.python.org/3/library/timeit.html, for example:
def foo():
return "hello"
timeit.timeit(foo,number=10)
Where number is number of repetition of time measure.
Related
I'm having trouble with a loop writing to a text file. I'm trying to create a tab delimited text file that writes an ID, date, time, sequence number, and text from a transcript to a line, then starts a new line every time it reaches bold text.
When there is only 1 ID in my company_list, everything works great and it produces this example below:
However, as soon as I add an additional ID to the company_list, it produces this:
It looks like when a second company ID is added, that a new line is placed after every ID for some unknown reason. What's even weirder is that when the loop runs the last company ID in the list, that data is formatted correctly. There are no errors produced at all. If anyone has any idea what is going on here I would really appreciate it.
Code snippet below:
company_list = open('Company_List.txt')
for line in company_list:
company_id = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[#id='SearchTopBar']")))
company_id.send_keys(line + Keys.ENTER)
driver.implicitly_wait(10)
driver.find_element_by_link_text("Transcripts").click()
driver.implicitly_wait(10)
driver.find_element_by_partial_link_text("Q1 2018").click()
date = driver.find_element_by_xpath('//*[#id="ctl01__header__dateLabel"]').text
struct_time = time.strptime(date, "%A, %B %d, %Y %I:%M %p")
speaker = 1
p_tag = driver.find_elements_by_tag_name('p')
file = open("q1_2018.txt", "a", encoding='utf-8-sig')
for i in range(3,len(p_tag) - 6):
element = driver.find_element_by_xpath('//*[#id="ctl01__bodyRow"]/td/p[' + str(i) + ']')
weight = int(element.value_of_css_property('font-weight'))
if weight == 700:
file.write('\n' + line + '\t' + str(struct_time[0]) + str(struct_time[1]) + str(struct_time[2]) + '\t' + str(struct_time[3]) + str(struct_time[4]) + '\t' + str(speaker) + '\t')
file.write(driver.find_element_by_xpath('//*[#id="ctl01__bodyRow"]/td/p[' + str(i + 1) + ']').text + ' ')
speaker = speaker + 1
else:
file.write(driver.find_element_by_xpath('//*[#id="ctl01__bodyRow"]/td/p[' + str(i) + ']').text + ' ')
file.close()
I have 130 lines of code in which part except from line 79 to 89 work fine like compiles in ~0.16 seconds however after adding function which is 10 lines(between 79-89) it works in 70-75 seconds. In that function the data file(u.data) is 100000 lines of numerical data in this format:
>196 242 3 881250949
4 grouped numbers in every line. The thing is that when I ran that function in another Python file while testing (before implementing it in the main program) it showed that it works in 0.15 seconds however when I implemented it in main one (same code) it takes whole program 70 seconds almost.
Here is my code:
""" Assignment 5: Movie Reviews
Date: 30.12.2016
"""
import os.path
import time
start_time = time.time()
""" FUNCTIONS """
# Getting film names in film folder
def get_film_name():
name = ''
for word in read_data.split(' '):
if ('(' in word) == False:
name += word + ' '
else:
break
return name.strip(' ')
# Function for removing date for comparison
def throw_date(string):
a_list = string.split()[:-1]
new_string = ''
for i in a_list:
new_string += i + ' '
return new_string.strip(' ')
def film_genre(film_name):
oboist = []
genr_list = ['unknown', 'Action', 'Adventure', 'Animation', "Children's", 'Comedy', 'Crime', 'Documentary', 'Drama',
'Fantasy',
'Movie-Noir', 'Horror', 'Musical', 'Mystery', 'Romance', 'Sci-Fi', 'Thriller', 'War', 'Western']
for item in u_item_list:
if throw_date(str(item[1])) == film_name:
for i in range(4, len(item)):
oboist.append(item[i])
dictionary = dict(zip(genr_list, oboist))
genres = ''
for key, value in dictionary.items():
if value == '1':
genres += key + ' '
return genres.strip(' ')
def film_link(film_name):
link = ''
for item in u_item_list:
if throw_date(str(item[1])) == film_name:
link += item[3]
return link
def film_review(film_name):
review = ''
for r, d, filess in os.walk('film'):
for fs in filess:
fullpat = os.path.join(r, fs)
with open(fullpat, 'r') as a_file:
data = a_file.read()
if str(film_name).lower() in str(data.split('\n', 1)[0]).lower():
for i, line in enumerate(data):
if i > 1:
review += line
a_file.close()
return review
def film_id(film_name):
for film in u_item_list:
if throw_date(film[1]) == film_name:
return film[0]
def total_user_and_rate(film_name):
rate = 0
user = 0
with open('u.data', 'r') as data_file:
rate_data = data_file.read()
for l in rate_data.split('\n'):
if l.split('\t')[1] == film_id(film_name):
user += 1
rate += int(l.split('\t')[2])
data_file.close()
print('Total User:' + str(int(user)) + '\nTotal Rate: ' + str(rate / user))
""" MAIN CODE"""
review_file = open("review.txt", 'w')
film_name_list = []
# Look for txt files and extract the film names
for root, dirs, files in os.walk('film'):
for f in files:
fullpath = os.path.join(root, f)
with open(fullpath, 'r') as file:
read_data = file.read()
film_name_list.append(get_film_name())
file.close()
with open('u.item', 'r') as item_file:
item_data = item_file.read()
item_file.close()
u_item_list = []
for line in item_data.split('\n'):
temp = [word for word in line.split('|')]
u_item_list.append(temp)
film_name_list = [i.lower() for i in film_name_list]
updated_film_list = []
print(u_item_list)
# Operation for review.txt
for film_data_list in u_item_list:
if throw_date(str(film_data_list[1]).lower()) in film_name_list:
strin = film_data_list[0] + " " + film_data_list[1] + " is found in the folder" + '\n'
print(film_data_list[0] + " " + film_data_list[1] + " is found in the folder")
updated_film_list.append(throw_date(str(film_data_list[1])))
review_file.write(strin)
else:
strin = film_data_list[0] + " " + film_data_list[1] + " is not found in the folder. Look at " + film_data_list[
3] + '\n'
print(film_data_list[0] + " " + film_data_list[1] + " is not found in the folder. Look at " + film_data_list[3])
review_file.write(strin)
total_user_and_rate('Titanic')
print("time elapsed: {:.2f}s".format(time.time() - start_time))
And my question is what can be the reason for that? Is the function
("total_user_and_rate(film_name)")
problematic? Or can there be other problems in other parts? Or is it normal because of the file?
I see a couple of unnecessary things.
You call film_id(film_name) inside the loop for every line of the file, you really only need to call it once before the loop.
You don't need to read the file, then split it to iterate over it, just iterate over the lines of the file.
You split each line twice, just do it once
Refactored for these changes:
def total_user_and_rate(film_name):
rate = 0
user = 0
f_id = film_id(film_name)
with open('u.data', 'r') as data_file:
for line in data_file:
line = line.split('\t')
if line[1] == f_id:
user += 1
rate += int(line[2])
data_file.close()
print('Total User:' + str(int(user)) + '\nTotal Rate: ' + str(rate / user))
In your test you were probably testing with a much smaller u.item file. Or doing something else to ensure film_id was much quicker. (By quicker, I mean it probably ran on the nanosecond scale.)
The problem you have is that computers are so fast you didn't realise when you'd actually made a big mistake doing something that runs "slowly" in computer time.
If your if l.split('\t')[1] == film_id(film_name): line takes 1 millisecond, then when processing a 100,000 line u.data file, you could expect your total_user_and_rate function to take 100 seconds.
The problem is that film_id iterates all your films to find the correct id for every single line in u.data. You'd be lucky, if the the film_id you're looking for is near the beginning of u_item_list because then the function would return in probably less than a nanosecond. But as soon as you run your new function for a film near the end of u_item_list, you'll notice performance problems.
wwii has explained how to optimise the total_user_and_rate function. But you could also gain performance improvements by changing u_item_list to use a dictionary. This would improve the performance of functions like film_id from O(n) complexity to O(1). I.e. it would still run on the nanosecond scale no matter how many films are included.
Brand new to programming but very enjoyable challenge.
Here's a question which I suspect may be caused by a misunderstanding of python loops.
System info: Using notepad++ and IDLE python 3.4.3 on Win 7 32-bit
My solution is to open 1 database, use it to look for a correct master entry from database 2, pulls a index number (task_no), then write a 3rd file identical to the first database, this time with the correct index number.
My problem is that it performs 1st and 2nd loop correctly, then on the 2nd iteration of loop 1, tries to perform a block in loop 2 while iterating through the rows of loop 1, not the task_rows of loop 2.
footnote: Both files are quite large (several MB) so I'm note sure if storing them in memory is a good idea.
This was a relevant question that I found closest to this problem:
python nested loop using loops and files
What I got out of it was that I tried moving the file opening within the 1st loop, but the problem persists. Something to do with how I'm using CSV reader?
I also have the sinking suspicion that there may be a root cause in problem solving so I am welcome to suggestions for alternative ways to solve the problem.
Thanks in advance!
The gist:
for row in readerCurrentFile: #LOOP 1
# iterates through readerCurrentFile to define search variables
[...]
for task_row in readerTaskHeader: #LOOP 2
# searches each row iteratively through readerTaskHeader
# Match compid
#if no match, continue <<<- This is where it goes back to 1st loop
[...]
# Match task frequency
#if no match, continue
[...]
# once both of the above matches check out, will grab data (task_no from task_row[0]
task_no = ""
task_no = task_row[0]
if task_row:
break
[...]
# writes PM code
print("Successful write of PM schedule row")
print(compid + " " + dict_freq_names[str(pmfreqx) + str(pmfreq)] + ": " + pmid + " " + task_no)
The entire code:
import csv
import re
#Writes schedule
csvNewPMSchedule = open('new_pm_schedule.csv', 'a', newline='')
writerNewPMSchedule = csv.writer(csvNewPMSchedule)
# Dictionaries of PM Frequency
def re_compile_dict(d,f):
for k in d:
d[k] = re.compile(d[k], flags=f)
dict_month = {60:'Quin',36:'Trien',24:'Bi-An',12:'Annual(?<!Bi-)(?<!Semi-)',6:'Semi-An',3:'Quart',2:'Bi-Month',1:'Month(?<!Bi-)'}
dict_week = {2:'Bi-Week',1:'Week(?<!Bi-)'}
dict_freq_names = {'60Months':'Quintennial','36Months':'Triennial','24Months':'Bi-Annual','12Months':'Annual','6Months':'Semi-Annual','3Months':'Quarterly','2Months':'Bi-Monthly','1Months':'Monthly','2Weeks':'Bi-Weekly','1Weeks':'Weekly'}
re_compile_dict(dict_month,re.IGNORECASE)
re_compile_dict(dict_week, re.IGNORECASE)
# Unique Task Counter
task_num = 0
total_lines = 0
#Error catcher
error_in_row = []
#Blank out all rows
pmid = 0
compid = 0
comp_desc = 0
pmfreqx = 0
pmfreq = 0
pmfreqtype = 0
# PM Schedule Draft (as provided by eMaint)
currentFile = open('pm_schedule.csv', encoding='windows-1252')
readerCurrentFile = csv.reader(currentFile)
# Loop 1
for row in readerCurrentFile:
if row[0] == "pmid":
continue
#defines row items
pmid = row[0]
compid = row[1]
comp_desc = row[2]
#quantity of pm frequency
pmfreqx_temp = row[3]
#unit of pm frequency, choices are: Months, Weeks
pmfreq = row[4]
#pmfreqtype is currently only static not sure what other options we have
pmfreqtype = row[5]
#pmnextdate is the next scheduled due date from this one. we probably need logic later that closes out any past due date
pmnextdate = row[6]
# Task Number This is what we want to change
# pass
# We want to change this to task header's task_desc
sched_task_desc = row[8]
#last done date
last_pm_date = row[9]
#
#determines frequency search criteria
#
try:
pmfreqx = int(pmfreqx_temp)
except (TypeError, ValueError):
print("Invalid PM frequency data, Skipping row " + pmid)
error_in_row.append(pmid)
continue
#
#defines frequency search variable
#
freq_search_var = ""
if pmfreq == "Weeks":
freq_search_var = dict_week[pmfreqx]
elif pmfreq == "Months":
freq_search_var = dict_month[pmfreqx]
if not freq_search_var:
print("Error in assigning frequency" + compid + " " + str(pmfreqx) + " " + pmfreq)
error_in_row.append(pmid)
continue
#defines Equipment ID Search Variable
print(compid + " frequency found: " + str(pmfreqx) + " " + str(pmfreq))
compid_search_var = re.compile(compid,re.IGNORECASE)
#
# Matching function - search taskHeader for data
#
#PM Task Header Reference
taskHeader = open('taskheader.csv', encoding='windows-1252')
readerTaskHeader = csv.reader(taskHeader)
for task_row in readerTaskHeader:
# task_row[0]: taskHeader pm number
# task_row[1]: "taskHeader task_desc
# task_row[2]: taskHeader_task_notes
#
# search for compid
compid_match = ""
compid_match = compid_search_var.search(task_row[1])
if not compid_match:
print(task_row[1] + " does not match ID for " + compid + ", trying next row.") #debug 2
continue # <<< STOPS ITERATING RIGHT OVER HERE
print("Found compid " + task_row[1]) # debug line
#
freq_match = ""
freq_match = freq_search_var.search(task_row[1])
if not freq_match:
print(task_row[1] + " does not match freq for " + compid + " " + dict_freq_names[str(pmfreqx) + str(pmfreq)] + ", trying next row.") #debug line
continue
print("Frequency Match: " + compid + " " + dict_freq_names[str(pmfreqx) + str(pmfreq)]) # freq debug line
#
task_no = ""
print("Assigning Task Number to " + task_row[0])
task_no = task_row[0]
if task_row:
break
#
#error check
#
if not task_no:
print("ERROR IN SEARCH " + compid + " " + pmid)
error_in_row.append(pmid)
continue
#
# Writes Rows
#
writerNewPMSchedule.writerow([pmid,compid,comp_desc,pmfreqx,pmfreq,pmfreqtype,pmnextdate,task_no,sched_task_desc,last_pm_date])
print("Successful write of PM schedule row")
print(compid + " " + dict_freq_names[str(pmfreqx) + str(pmfreq)] + ": " + pmid + " " + task_no)
print("==============")
# Error reporting lined out for now
# for row in error_in_row:
# writerNewPMSchedule.writerow(["Error in row:",str(error_in_row[row])])
# print("Error in row: " + str(error_in_row[row]))
print("Finished")
I put trailing print() methods right next to my write() method lines at the end of my code to test why my output files were incomplete. But, the print() output is "all the stuff" I expect; while the write() output is off by a confusing amount (only 150 out of 200 'things'). Reference Image of Output: IDLE versus external output file
FYI: Win 7 64 // Python 3.4.2
My modules take an SRT captions file ('test.srt') and returns a list object I create from it; in particular, one with 220 list entries of the form: [[(index), [time], string]]
times = open('times.txt', 'w')
### A portion of Riobard's SRT Parser: srt.py
import re
def tc2ms(tc):
''' convert timecode to millisecond '''
sign = 1
if tc[0] in "+-":
sign = -1 if tc[0] == "-" else 1
tc = tc[1:]
TIMECODE_RE = re.compile('(?:(?:(?:(\d?\d):)?(\d?\d):)?(\d?\d))?(?:[,.](\d?\d?\d))?')
match = TIMECODE_RE.match(tc)
try:
assert match is not None
except AssertionError:
print(tc)
hh,mm,ss,ms = map(lambda x: 0 if x==None else int(x), match.groups())
return ((hh*3600 + mm*60 + ss) * 1000 + ms) * sign
# my code
with open('test.srt') as f:
file = f.read()
srt = []
for line in file:
splitter = file.split("\n\n")
# SRT splitter
i = 0
j = len(splitter)
for items in splitter:
while i <= j - 2:
split_point_1 = splitter[i].index("\n")
split_point_2 = splitter[i].index("\n", split_point_1 + 1)
index = splitter[i][:split_point_1]
time = [splitter[i][split_point_1:split_point_2]]
time = time[0][1:]
string = splitter[i][split_point_2:]
string = string[1:]
list = [[(index), [time], string]]
srt += list
i += 1
# time info outputter
i = 0
j = 1
for line in srt:
if i != len(srt) - 1:
indexer = srt[i][1][0].index(" --> ")
timein = srt[i][1][0][:indexer]
timeout = srt[i][1][0][-indexer:]
line_time = (tc2ms(timeout) - tc2ms(timein))/1000
space_time = ((tc2ms((srt[j][1][0][:indexer]))) - (tc2ms(srt[i][1][0][-indexer:])))/1000
out1 = "The space between Line " + str(i) + " and Line " + str(j) + " lasts " + str(space_time) + " seconds." + "\n"
out2 = "Line " + str(i) + ": " + str(srt[i][2]) + "\n\n"
times.write(out1)
times.write(out2)
print(out1, end="")
print(out2)
i += 1
j += 1
else:
indexer = srt[i][1][0].index(" --> ")
timein = srt[i][1][0][:indexer]
timeout = srt[i][1][0][-indexer:]
line_time = (tc2ms(timeout) - tc2ms(timein))/1000
outend = "Line " + str(i) + ": " + str(srt[i][2]) + "\n<End of File>"
times.write(outend)
print(outend)
My two write() method output files, respectively, only print out either ~150 or ~200 items of the 220 things it otherwise correctly prints to the screen.
You want to close your times file when done writing; operating systems use write buffers to speed up file I/O, collecting larger blocks of data to be written to disk in one go; closing the file flushes that buffer:
times.close()
Consider opening the file in a with block:
with open('times.txt', 'w') as times:
# all code that needs to write to times
I have a file, which I am writing data to, however I would like the naming convention to have the time, the file was created at and then stream to the same file for one hour before creating a new text file.
My problem is that I am using a while loop to create the text file while reading from a GPIO and so when the code runs, it creates a new text file every time it moves through the loop. How can I create the file, then write to the existing file for a predetermined amount of time?
import spidev
import time
import datetime
import os
spi = spidev.SpiDev()
spi.open(0,0) #open port 0 of the SPI
count = 0
def timeStamp(fname, fmt ='%Y-%m-%d-%H-%M-%S_{fname}'):
#this function adds the time and date
return datetime.datetime.now().strftime(fmt).format(fname = fname)
def alarm_check(int):
#this function checks the value read in by the SPI
#creating an alarm if the value is 0
if int == 0:
return ("System Alarm!")
else:
return ("System OK")
def write_to_file(int):
# this function will write the current value to a file,
# the time it was read in
# and if the system was im alarm
with open (('SPI_Input_Values'), 'a') as output:
output.write("Input = " + str(int) + '\t' + timeStamp('ADC') + '\t\t' + str(alarm_check(int)) + '\n')
def readadc(adcnum):
#this function will open the SPI and read it to see the current value
# this will then be written to a text value
# using the write_to_file function
if adcnum > 7 or adcnum < 0:
return -1
r = spi.xfer2([1,8 + adcnum << 4,0])
adcout = ((r[1] & 3) << 8) + r[2]
return adcout
while True:
Inp1 = int(round(readadc(0)/10.24)) # defines Inp1 as an integer to be read in
write_to_file(Inp1)
count = count +1
time.sleep(0.1) # puts the systemm to sleep for 0.1 seconds
You need to dynamically create your filename. At the moment it was a hardcoded string. Here's an example:
fname = 'SPI_Input_Values'
fmt ='%Y-%m-%d-%H'
date_str = datetime.datetime.now().strftime(fmt)
file_name = date_str + "_" + fname
with open ((file_name, 'a') as output:
output.write("Input = " + str(int) + '\t' + timeStamp('ADC') + '\t\t' + str(alarm_check(int)) + '\n')
This will constantly append to the file that represents the current hour. When the next hour begins, it will automatically create a new file and begin appending data to it instead.
Something like:
def write_to_file(output, int):
output.write("Input = " + str(int) + '\t' + timeStamp('ADC') + '\t\t' + str(alarm_check(int)) + '\n')
with open (('SPI_Input_Values'), 'a') as output:
while True:
Inp1 = int(round(readadc(0)/10.24)) # defines Inp1 as an integer to be read in
write_to_file(output, Inp1)
count = count +1
time.sleep(0.1)