Results 1 to 3 of 3

Thread: when generate 10000 matrix bar code it take too much time How to make generating take

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    37

    when generate 10000 matrix bar code it take too much time How to make generating take

    Problem

    When generating matrix 2d bar code it take per 10000 files matrix

    bar code generating it take 1 minute

    so that how to generating matrix bar code 2d in less time as

    seconds .

    my code as below under button generating :

    Code:
    Class1 CLS = new Class1();
                    DataTable dt = CLS.ShowalldataSerial(textBox4.Text);
    
                    for (int i = 0; i <= Convert.ToInt32(textBox1.Text); i++)
                    {
                        Serial = SRL.Rnd().ToString();
                        txt = "UserID" + dt.Rows[0][0] + "FirmName" + dt.Rows[0][1] + "OrderNo" + dt.Rows[0][2] + "BtachNo" + dt.Rows[0][3] + "Quantity" + dt.Rows[0][4] + "ProductName" + dt.Rows[0][5] + "SerialNo" + Serial;
    
                        dm.DM(txt, Color.FromName(comboBox1.SelectedItem.ToString()), Color.White).Save(root + "\\" + Serial + ".emf", System.Drawing.Imaging.ImageFormat.Emf);
    
                    }
                    MessageBox.Show("Records generated success ");
    when create 10000 in textbox1 it take minute if i write

    200000 in textbox1 it take 20 minutes .

    Code working without any problem and give me result what i need

    but it slowly generating data matrix per big quantities

    so that what i do to make generating matrix bar code very fast .

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,936

    Re: when generate 10000 matrix bar code it take too much time How to make generating

    When you are trying to make a loop faster, there are several things you can do.

    One is to find things you are doing inside the loop that don't need to be there, and can be moved outside. For example, I assume comboBox1.SelectedItem doesn't change inside the loop, so I would expect Color.FromName(comboBox1.SelectedItem.ToString()) to return the same value every time, in which case you can move that outside the loop and just store the result to a variable (which you then use inside the loop); this should also make your code easier to read. Similar applies to root + "\\" but that would be a smaller gain.

    Another thing you can do is think about alternative methods, so for example there might be a faster way to set the value of txt (possibly a StringBuilder or something else), but I haven't needed to optimise that kind of thing in C# so I'm not sure.


    A very important thing to do is think about the function calls you are making, which can easily be very slow (depending on what they are doing). In this case you have SRL.Rnd() and dm.DM() and .Save(). I can only guess at what they do, but I suspect that .Rnd is the fastest, and .Save is the slowest.

    Don't just use my suspicions, find out how much time each of them takes (by using a profiler, or the StopWatch class, etc), and then think about what to do next... if .Save is something that must be done (and there is no alternative way to do it), and you find that it takes 99% of the time, it isn't worth worrying too much about the other code.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,689

    Re: when generate 10000 matrix bar code it take too much time How to make generating

    Is it my imagination or are you not even using 'i' inside that loop? Should you maybe be using 'dt.Rows[i]' rather than 'dt.Rows[0]'? In keeping with si's adavice, you should also be getting that DataRow only once and assigning it to a variable, then using the variable multiple times. You should basically NEVER use a complex expression multiple times if you know that it will yield the same result every time. Use the expression once, assign the result to a variable and then reuse the variable.

    Anyway, once you've considered si's advice and cleaned up your code, you might also look at implementing some parallelism, e.g. calling Parallel.For instead of using a standard 'for' loop.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width