Hi
I know that using eval is highly discouraged, but for the below test case it seems to be faster than other options. Can you help me explain this and let me know which alternative you prefer?
struct1.data = 2*ones(1e3);struct2.data = 4*ones(1e3);struct(1).data = 2*ones(1e3);struct(2).data = 4*ones(1e3);selected_struct_str = 'struct1';selected_struct_num = 1;num_iter = 10;ticfor i = 1:num_iter test1 = doWork1(struct1, struct2, selected_struct_str);endtocticfor i = 1:num_iter test2 = doWork2(struct1, struct2, selected_struct_num);endtocticfor i = 1:num_iter test3 = doWork3(struct, selected_struct_num);endtocfunction data = doWork1(struct1, struct2, selected_struct) data = eval([selected_struct '.data']);endfunction data = doWork2(struct1, struct2, selected_struct_num) switch selected_struct_num case 1 data = struct1.data; case 2 data = struct2.data; endendfunction data = doWork3(struct, selected_struct_num) data = struct(selected_struct_num).data;end
I am getting the following output:
>> eval_testElapsed time is 0.000418 seconds.Elapsed time is 0.001516 seconds.Elapsed time is 0.001594 seconds.>> eval_testElapsed time is 0.000384 seconds.Elapsed time is 0.001027 seconds.Elapsed time is 0.001186 seconds.>> eval_testElapsed time is 0.000558 seconds.Elapsed time is 0.001280 seconds.Elapsed time is 0.001304 seconds.>> eval_testElapsed time is 0.000641 seconds.Elapsed time is 0.001754 seconds.Elapsed time is 0.001802 seconds.>> eval_testElapsed time is 0.000357 seconds.Elapsed time is 0.001117 seconds.Elapsed time is 0.001430 seconds.>> eval_testElapsed time is 0.000533 seconds.Elapsed time is 0.001231 seconds.Elapsed time is 0.001234 seconds.
Best Answer