Transaction commit is time consuming

Transaction commit is time consuming

vidya.zende
Contributor Contributor
1,380 Views
9 Replies
Message 1 of 10

Transaction commit is time consuming

vidya.zende
Contributor
Contributor

Hi , Good morning.

 

I am facing a issue for Transaction commit, I am setting one shared parameter value for an element. Let's say I have 10 elements so for each element I am adding a counter + some string as parameter value and setting it to an element. But found that even for lesser number of elements the Transaction commit taking time more than enough. If I have not increment the counter in loop then transaction commit is quite faster, lets say 3 times faster than previous. But I have to increment the counter to get proper value for parameter.

 

Here is snip of my code

using (Transaction transaction = new Transaction(doc, "Set Parameters"))
{
transaction.Start();

int counter=0;

foreach (var element in elements)
{

string sample =counter.ToString() +"_Test";

element.Source.get_Parameter(testguid).Set(sample);

counter++;

}

transaction.Commit();

}

 

if I skip "counter++" my code is faster.

Please help me out to understand what's going wrong on my side.

0 Likes
1,381 Views
9 Replies
Replies (9)
Message 2 of 10

Mohamed_Arshad
Advisor
Advisor

HI @vidya.zende 

 

    There are Two types of method to increase your efficiency in terms of List. I didn't understand what is source.getParameter keyword in your code ?

 

1. Use Forloop instead of foreach loop

 

 

 using (Transaction transaction = new Transaction(doc, "Set Parameters"))
            {
                transaction.Start();

                for (int index = 0; index < elements.Count; index++)
                {
                    elements[index].Source.get_Parameter(testguid).Set($"{index}_Test");
                }

                transaction.Commit();
            }

 

 

2. Use IEnumerator

 

 

  using (Transaction transaction = new Transaction(doc, "Set Parameters"))
            {
                transaction.Start();

                var elem = elements.GetEnumerator();

                int counter = 0;

                while (elem.MoveNext())
                {
                    elem.Current.Source.get_Parameter(testguid).Set($"{counter}_Test");

                    counter++;
                }

                transaction.Commit();
            }

 

 

Test two methods and comment which is efficient. Hope this will helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 3 of 10

ankofl
Advocate
Advocate

Hi) Try to format the code, it will improve its perception)

using (Transaction transaction = new Transaction(doc, "Set Parameters"))
{
transaction.Start();

int counter=0;

foreach (var element in elements)
{

string sample =counter.ToString() +"_Test";

element.Source.get_Parameter(testguid).Set(sample);

counter++;

}

transaction.Commit();

}

The delays described by you in the code are characteristic if ".Commit();" is executed inside each iteration of the loop. The application of the transaction at the same time at the end of the cycle should be equally fast, regardless of whether the counter increases inside the cycle or not.
Correct me, if I misunderstood you

 

0 Likes
Message 4 of 10

vidya.zende
Contributor
Contributor

Hi @Mohamed_Arshad , Thank you so much for your reply.

 

The source is nothing but the element only to which I am setting parameter value.

 

I have the counter that has some previous value from first run, so I have to carry forward that counter. In such case the simple for loop is not useful.

I have tried second approach but it seems taking same time as my old code.

0 Likes
Message 5 of 10

vidya.zende
Contributor
Contributor

Hi @ankofl Thanks for the reply.

 

The transaction commit is takes place outside the for loop so it executes only once.

I am also afraid about this behavior of counter increment but I have tested the time difference with/without counter increment, it is always more in the case where counter is incrementing.

Message 6 of 10

ankofl
Advocate
Advocate

Wall, and in the absence of iterations, is there an addition to the index to the row? Or is the parameter simply filled in with the string specified at the compilation stage? I think in this case, all the delay occurs during the formatting of the string. 

string sample =counter.ToString() +"_Test";
element.Source.get_Parameter(testguid).Set(sample);

Compare with this:

element.Source.get_Parameter(testguid).Set("_Test");



0 Likes
Message 7 of 10

Mohamed_Arshad
Advisor
Advisor

Hi @jeremytammik

 Can you please guide us in the Optimization, If we have multiple Iterations, How to increase the speed of the process. That will really helps us 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 8 of 10

jeremy_tammik
Alumni
Alumni

I find the differences described above very hard to believe. In fact, I do not believe them. Please submit a complete minimal reproducible case including benchmarking code proving the performance differences that you describe. Until I see complete code including benchmark timers, I see no possible way to advise.

     

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 9 of 10

Mohamed_Arshad
Advisor
Advisor

@vidya.zende 

    

        Can you please provide a complete code with a sample file (which has more elements). So that @jeremy_tammik  guide us to achieve the Speed. 


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 10 of 10

vidya.zende
Contributor
Contributor

Hi @jeremy_tammik ,@Mohamed_Arshad 

 

Thank you so much for your valuable time.

My apologized, actually after @jeremy_tammik post I had tried to replicate the issue in a sample code with other family instances to set the parameter value in a loop with counter increment. It works fine and faster for same even more number of elements. Than I had realized that the issue with my family that I am using, I had recreated the shared parameters and it works. Now it is setting the parameter values in a faster manner. 

 

Next time I will use this hint to replicate the issue first in a sample code and observe the behavior. 

0 Likes