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])