0

I'm running some calculations on a CSV file. Can anyone help me explain why method 1 is about 3 times faster than method 2? I wanted to make my code more generic and sustainable so I didn't want to hardcode like in method 1, but the performance has dropped drastically once I switched to method 2 and I can't figure out why.

Method 1:

for row in itertools.islice(csv_f, 0, period):
    voltage_rail1 = float(row[rail1_index])
    voltage_rail1_q.append(voltage_rail1)
    current_rail1 = float(row[current_index])
    current_rail1_q.append(current_rail1)
    power_rail1_q.append(voltage_rail1*current_rail1)

    voltage_rail2 = float(row[rail2_index])
    voltage_rail2_q.append(voltage_rail2)
    current_rail2 = float(row[current_index])
    current_rail2_q.append(current_rail2)
    power_rail2_q.append(voltage_rail2*current_rail2)

Method 2:

rails_d = OrderedDict()
rails_d['rail1'] = 0
rails_d['rail2'] = 1

for row in itertools.islice(csv_f, 0, period):
    for rail in rails_d:
        voltage_d[rail] = float(row[voltage_column_index_d[rail]])
        voltage_dq[rail].append(voltage_d[rail])
        current_d[rail] = float(row[current_column_index_d[rail]])
        current_dq[rail].append(current_d[rail])
        power_dq[rail].append(voltage_d[rail]*current_d[rail])
Colin
  • 415
  • 3
  • 14

1 Answers1

0

Do you really need a dictionary? From both snippets, it looks like you're using only the key (or the index 0/1) Maybe a list will speed things up a bit

for rail in ['rail1','rail2']:

This answer discusses iteration performance in-depth.

Side-note, have you tried product from itertools

for rail, row in itertools.product(['rail1','rail2'], itertools.islice(csv_f, 0, period)):
Community
  • 1
  • 1
Obsidian
  • 515
  • 3
  • 10
  • Thanks for your suggestions. I've implemented both but the performance looks to be about the same. `rails_l = ['rail1', 'rail2'] for rail, row in itertools.product(rails_l, itertools.islice(csv_f, 0, period)): voltage_d[rail] = float(row[voltage_column_index_d[rail]]) voltage_dq[rail].append(voltage_d[rail]) current_d[rail] = float(row[current_column_index_d[rail]]) current_dq[rail].append(current_d[rail]) power_dq[rail].append(voltage_d[rail]*current_d[rail])` – Colin Feb 11 '16 at 03:39