question

Ankur A3 avatar image
0 Likes"
Ankur A3 asked Ankur A3 commented

Making code faster using Global Variable?

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")[Table("Inputs").numRows][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)[Table(GV_Inputs).numRows][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!

FlexSim 23.1.0
codescriptmodel optimization
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Jason Lightfoot avatar image
0 Likes"
Jason Lightfoot answered Ankur A3 commented

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;

· 5
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Ankur A3 avatar image Ankur A3 commented ·
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 0 ·
Felix Möhlmann avatar image Felix Möhlmann Ankur A3 commented ·

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 0 ·
capture1.png (4.8 KiB)
capture2.png (11.9 KiB)
Ankur A3 avatar image Ankur A3 Felix Möhlmann commented ·
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 0 ·
Show more comments