Making code faster using Global Variable?

Making code faster using Global Variable?

rajankur6494
Collaborator Collaborator
236 Views
6 Replies
Message 1 of 7

Making code faster using Global Variable?

rajankur6494
Collaborator
Collaborator

[ FlexSim 23.1.0 ]

Hi Team,

I am trying to optimize my model in terms of code. But I think I need some more understanding to make it successful.

Let say I have this code in my model:

Table("Inputs").addRow();

Table("Inputs")

[1]=Table("Inputs").numRows;


Now, I have taken the reference of global table as global variable and I am using it in my model:

Table(GV_Inputs).addRow();

Table(GV_Inputs)

[1]=Table(GV_Inputs).numRows;


I read in many post that if we use global variable, it works better. But it doesn't make any time difference for me.

Whether I am using it in the right way?

Thank you!

0 Likes
Accepted solutions (1)
237 Views
6 Replies
Replies (6)
Message 2 of 7

jason_lightfoot_adsk
Autodesk
Autodesk
Accepted solution

Adding 3.8 million rows using the method you outline takes 6.416 seconds on my machine.

Using a Table local variable reduces that to 5.3 seconds.

Then changing the table to a bundle reduces it to 0.32 seconds.

The test script:

double start=realtime(1)+realtime(2)/1000;
Table t=Table("GlobalTable1");
for (int n=3800000;n>0;n--){
    t.addRow();
    t[t.numRows][1]=t.numRows;
}
return realtime(1)+realtime(2)/1000- start;

0 Likes
Message 3 of 7

rajankur6494
Collaborator
Collaborator
Hi @Jason Lightfoot,

Thanks for your quick response. I have 2 questions here:

1. What do you mean by bundle here?

2. I have to use the global table in 20 different object script in my model. what would you suggest in that case to make the run faster?

Thank you!

0 Likes
Message 4 of 7

moehlmann_fe
Advocate
Advocate

1. A bundle is a different way table data can be stored in FlexSim. By default, each cell of a global table is stored as a separate node. A bundle stores the entire table data in a single node. Using a bundle comes with some restrictions, which FlexSim will inform you about when you change a global table to a bundle. But access times will be much faster and the bundle columns can be indexed to significantly speed up queries.

capture1.png

2. If you think that accessing the table is slowing down the model, you can link a global variable to the data node of the table.

capture2.png

Table tab = TableLink;

Though as Jason's test shows, the gain from this is minor compared to using a bundle table instead of a node table.

0 Likes
Message 5 of 7

rajankur6494
Collaborator
Collaborator
Thank you @Felix Möhlmann

I would like to ask 1 question here:

What will be the difference if we are linking global table directly or we are linking data node?

Thank you!

0 Likes
Message 6 of 7

moehlmann_fe
Advocate
Advocate
I assume by "global table directly" you mean the table object? If you link that you would still need to get a reference to the data node by navigating its tree. You can only cast the data node (or any other node with bundle data or a fitting subnode structure) as a table.

The constructor Table() also allows you to pass in the name of the table and then finds the data node automatically, but that is what you are trying to avoid, no?

0 Likes
Message 7 of 7

rajankur6494
Collaborator
Collaborator
Yes, you are correct.

@Felix Möhlmann

Thanks for your explanation!

0 Likes