I am refactoring a small project which bases on openpyxl to analyze .xlsx inputs. I am trying to reduce time needed for it to do its job and currently trying methods for accessing cell values of Excel worksheet.
I tested two methods (code below) with timeit and one of them seems to be twice as fast as first one.
import timeit
from openpyxl.reader.excel import ExcelReader
samplesPath = r'path_to_excel_file'
workbook = ExcelReader(samplesPath)
workbook.read()
worksheet = workbook.wb['Sheet1']
def func1():
for i in range(1,10):
worksheet.cell(i,1).value = 'value'
def func2():
for i in range(1,10):
workbook.wb['Sheet1'].cell(i,1).value = 'value'
print(timeit.timeit(func1))
print(timeit.timeit(func2))
Timeit scores:
func1 = 29.23 func2 = 51.07
Can someone explain me the difference between saving worksheet as variable, and then accessing the cell value with it, and accessing it when calling a worksheet every time?
How does it vary in the background, and if there is even faster method?