Skip to content

Commit

Permalink
black: reformat dev examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Nov 16, 2024
1 parent 3cbc44b commit 4e25e6f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
43 changes: 21 additions & 22 deletions dev/performance/bench_excel_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@


def print_elapsed_time(module_name, elapsed):
""" Print module run times in a consistent format. """
"""Print module run times in a consistent format."""
print(f" {module_name:28s}: {elapsed:6.2f}")


def time_xlsxwriter():
""" Run XlsxWriter in default mode. """
"""Run XlsxWriter in default mode."""
start_time = perf_counter()

workbook = xlsxwriter.Workbook('xlsxwriter.xlsx')
workbook = xlsxwriter.Workbook("xlsxwriter.xlsx")
worksheet = workbook.add_worksheet()

for row in range(row_max // 2):
Expand All @@ -49,15 +49,14 @@ def time_xlsxwriter():
workbook.close()

elapsed = perf_counter() - start_time
print_elapsed_time('xlsxwriter', elapsed)
print_elapsed_time("xlsxwriter", elapsed)


def time_xlsxwriter_optimised():
""" Run XlsxWriter in optimised/constant memory mode. """
"""Run XlsxWriter in optimised/constant memory mode."""
start_time = perf_counter()

workbook = xlsxwriter.Workbook('xlsxwriter_opt.xlsx',
{'constant_memory': True})
workbook = xlsxwriter.Workbook("xlsxwriter_opt.xlsx", {"constant_memory": True})
worksheet = workbook.add_worksheet()

for row in range(row_max // 2):
Expand All @@ -69,11 +68,11 @@ def time_xlsxwriter_optimised():
workbook.close()

elapsed = perf_counter() - start_time
print_elapsed_time('xlsxwriter (constant_memory)', elapsed)
print_elapsed_time("xlsxwriter (constant_memory)", elapsed)


def time_openpyxl():
""" Run OpenPyXL in default mode. """
"""Run OpenPyXL in default mode."""
start_time = perf_counter()

workbook = openpyxl.workbook.Workbook()
Expand All @@ -85,14 +84,14 @@ def time_openpyxl():
for col in range(col_max):
worksheet.cell(row * 2 + 2, col + 1, row + col)

workbook.save('openpyxl.xlsx')
workbook.save("openpyxl.xlsx")

elapsed = perf_counter() - start_time
print_elapsed_time('openpyxl', elapsed)
print_elapsed_time("openpyxl", elapsed)


def time_openpyxl_optimised():
""" Run OpenPyXL in optimised mode. """
"""Run OpenPyXL in optimised mode."""
start_time = perf_counter()

workbook = openpyxl.workbook.Workbook(write_only=True)
Expand All @@ -105,48 +104,48 @@ def time_openpyxl_optimised():
num_data = [row + col for col in range(col_max)]
worksheet.append(num_data)

workbook.save('openpyxl_opt.xlsx')
workbook.save("openpyxl_opt.xlsx")

elapsed = perf_counter() - start_time
print_elapsed_time('openpyxl (optimised)', elapsed)
print_elapsed_time("openpyxl (optimised)", elapsed)


def time_pyexcelerate():
""" Run pyexcelerate in "faster" mode. """
"""Run pyexcelerate in "faster" mode."""
start_time = perf_counter()

workbook = pyexcelerate.Workbook()
worksheet = workbook.new_sheet('Sheet1')
worksheet = workbook.new_sheet("Sheet1")

for row in range(row_max // 2):
for col in range(col_max):
worksheet.set_cell_value(row * 2 + 1, col + 1, f"Row: {row} Col: {col}")
for col in range(col_max):
worksheet.set_cell_value(row * 2 + 2, col + 1, row + col)

workbook.save('pyexcelerate.xlsx')
workbook.save("pyexcelerate.xlsx")
elapsed = perf_counter() - start_time

print_elapsed_time('pyexcelerate', elapsed)
print_elapsed_time("pyexcelerate", elapsed)


def time_xlwt():
""" Run xlwt in default mode. """
"""Run xlwt in default mode."""
start_time = perf_counter()

workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet1')
worksheet = workbook.add_sheet("Sheet1")

for row in range(row_max // 2):
for col in range(col_max):
worksheet.write(row * 2, col, f"Row: {row} Col: {col}")
for col in range(col_max):
worksheet.write(row * 2 + 1, col, row + col)

workbook.save('xlwt.xls')
workbook.save("xlwt.xls")

elapsed = perf_counter() - start_time
print_elapsed_time('xlwt', elapsed)
print_elapsed_time("xlwt", elapsed)


print("")
Expand Down
17 changes: 8 additions & 9 deletions dev/performance/perf3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#
import os
import sys

try:
from time import process_time
except ImportError:
Expand All @@ -33,24 +34,23 @@


def print_elapsed_time(module_name, elapsed, optimised=False):
""" Print module run times in a consistent format. """
"""Print module run times in a consistent format."""
if optimised:
module_name += " (optimised)"
print(f" {module_name:22s}: {elapsed:6.2f}")


def time_xlsxwriter(optimised=False):
""" Run XlsxWriter in optimised/constant memory mode. """
"""Run XlsxWriter in optimised/constant memory mode."""
options = {}
module_name = "xlsxwriter"
if optimised:
options['constant_memory'] = True
options["constant_memory"] = True

start_time = process_time()
filename = 'xlsxwriter_opt.xlsx'
filename = "xlsxwriter_opt.xlsx"

workbook = xlsxwriter.Workbook(filename,
options=options)
workbook = xlsxwriter.Workbook(filename, options=options)
for r in range(sheets):
worksheet = workbook.add_worksheet()

Expand All @@ -69,11 +69,11 @@ def time_xlsxwriter(optimised=False):


def time_openpyxl(optimised=False):
""" Run OpenPyXL in default mode. """
"""Run OpenPyXL in default mode."""
module_name = "openpyxl"

start_time = process_time()
filename = 'openpyxl.xlsx'
filename = "openpyxl.xlsx"

workbook = openpyxl.Workbook(write_only=optimised)
for r in range(sheets):
Expand Down Expand Up @@ -113,4 +113,3 @@ def time_openpyxl(optimised=False):
time_openpyxl()
time_openpyxl(optimised=True)
print("")

5 changes: 2 additions & 3 deletions dev/performance/perf_pyx.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
start_time = perf_counter()

# Start of program being tested.
workbook = xlsxwriter.Workbook('py_ewx.xlsx',
{'constant_memory': optimise})
workbook = xlsxwriter.Workbook("py_ewx.xlsx", {"constant_memory": optimise})
worksheet = workbook.add_worksheet()

worksheet.set_column(0, col_max, 18)
Expand All @@ -60,4 +59,4 @@

# Print a simple CSV output for reporting.

print(f"{row_max * 2:6d}, {col_max:3d}, {elapsed:6.2f}, {total_size}")
print(f"{row_max * 2:6d}, {col_max:3d}, {elapsed:6.2f}, {total_size}")

0 comments on commit 4e25e6f

Please sign in to comment.