Basic For Loop speed test between C#, VB.Net and B4J

MarkusR

Well-Known Member
Licensed User
Longtime User
just for fun my c# counting in +1 steps + responsive somehow.
 

Peter Simpson

Expert
Licensed User
Longtime User
just for fun my c# counting in +1 steps + responsive somehow.

I'm not quite sure what yours is doing, as I said previous in VS2019 I just wanted a label that clearly shows it counting from 0 to 100000 using a for loop then stopping with the time showing.

B4X:
        private void CountUp()
        {
            var TimeElapsed = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i <= 100000; i++)
            {
                label1.Text = Convert.ToString(i);
                //label1.Invalidate();
                //label1.Update(); //2.9 seconds faster
                //label1.Refresh();//2.7 seconds faster
                Application.DoEvents();
            }
 
            TimeElapsed.Stop();
            Double Elapsed = TimeElapsed.ElapsedMilliseconds;
            label2.Text = $"Time elapsed was {(Elapsed / 1000).ToString("F")}";
            Console.WriteLine((Elapsed / 1000).ToString("#.##"));
        }

Anyway it's lager time now, I've been working hard all day and I need a break from it all ;)
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
Normally B4J speed is near Java speed. Isn't it?
While doing a uLaw compression function for @Jmu5667, my first iteration was an inline Java version of https://stackoverflow.com/a/23603738. I then did a pure B4X version. Testing showed that the B4X version was just as fast and at times faster than the inline version. So at least in this one case, B4J speed matched or beat the Java implementation of an iterative code. B4X version of uLaw: https://www.b4x.com/android/forum/t...sion-code-available-to-all.96695/#post-612931
 

MarkusR

Well-Known Member
Licensed User
Longtime User
I just wanted a label that clearly shows it counting from 0 to 100000 using a for loop then stopping with the time showing.
at my pc it seems B4J is 2,5x faster that c# but i can't see if the label is really painted for each text assignment.
 

Peter Simpson

Expert
Licensed User
Longtime User
at my pc it seems B4J is 2,5x faster that c# but i can't see if the label is really painted for each text assignment.

Viewing the labels and seeing what was going one is what slows it down, without labels and using just the for loop with nothing in it, counting to 100000000 only takes 0.02 seconds in C#.

B4X:
//Code below takes 0.31 seconds with no labels
            for (int i = 0; i <= 1000000000; i++)
            {
            }
 
Last edited:

Peter Simpson

Expert
Licensed User
Longtime User
your countup in c# was 25 seconds and in b4j it was 10 seconds.
If my memory serves me correctly, when I purchased my spare laptop to go abroad with 2 years ago, the processor was an i5-6300u which is a mobile processor. I'm presuming that you're using a desktop processor (but I could be wrong). I've not tested the speed on my i7 development laptop (it was encoding videos at the time), but obviously it would be much quicker especially as it's an i7 8th Gen and also 2 years newer.

But thank you for your feedback, I still want to know how you got 7 seconds though.

As was said in the first post, JavaFX appears to be a lot quicker.
B4X:
'B4J code was just
Sub CountUp
    Dim StartTime As Long = DateTime.Now  
  
    For i = 0 To 100000
        Label1.Text = i
        Sleep(0)
    Next
  
    Label2.Text = $"Time elapsed = ${NumberFormat2((DateTime.Now - StartTime) / 1000, 1, 2, 2, False)} seconds"$
    Log($"Time elapsed = ${NumberFormat2((DateTime.Now - StartTime) / 1000, 1, 2, 2, False)} seconds"$)  
End Sub

Once again remove the labels and the results are basically instant.

Cheers...
 

MarkusR

Well-Known Member
Licensed User
Longtime User
If my memory serves me correctly, when I purchased my spare laptop to go abroad with 2 years ago, the processor was an i5-6300u which is a mobile processor. I'm presuming that you're using a desktop processor (but I could be wrong). I've not tested the speed on my i7 development laptop (it was encoding videos at the time), but obviously it would be much quicker especially as it's an i7 8th Gen and also 2 years newer.

But thank you for your feedback, I still want to know how you got 7 seconds though.

As was said in the first post, JavaFX appears to be a lot quicker.


Once again remove the labels and the results are basically instant.

Cheers...

yes i have a desktop processor.
with labels it take 10 seconds and without 3,5, so the 3,5 seconds waste is sleep(0) :)

B4X:
Sub CountUp
    Dim StartTime As Long = DateTime.Now
 
    For i = 0 To 100000
        'Label1.Text = i
        Sleep(0)
    Next
 
    'Label2.Text = $"Time elapsed = ${NumberFormat2((DateTime.Now - StartTime) / 1000, 1, 2, 2, False)} seconds"$
    Log($"Time elapsed = ${NumberFormat2((DateTime.Now - StartTime) / 1000, 1, 2, 2, False)} seconds"$)
End Sub

7 seconds because i used a interval for doevents and write into the labels.
i know it was off topic :)
c#
B4X:
void Count() {
            Console.WriteLine("Start");

            var Start = DateTime.Now;

            var dateStart = DateTime.Now;
            TimeSpan diff;

            for (int i = 0; i <= 10000000; i++)
            {
                diff = DateTime.Now - dateStart;
                if (diff.TotalMilliseconds >= 250)
                {
                    dateStart = DateTime.Now;
                    label1.Text = i.ToString("000,000,000");
                    //label1.Refresh();
                    Application.DoEvents();
                }

            }

            label2.Text = (DateTime.Now - Start).TotalSeconds + " Sec.";

            Console.WriteLine("Ready.");
        }
 

Peter Simpson

Expert
Licensed User
Longtime User
c#
B4X:
void Count() {
            Console.WriteLine("Start");

            var Start = DateTime.Now;

            var dateStart = DateTime.Now;
            TimeSpan diff;

            for (int i = 0; i <= 10000000; i++)
            {
                diff = DateTime.Now - dateStart;
                if (diff.TotalMilliseconds >= 250)
                {
                    dateStart = DateTime.Now;
                    label1.Text = i.ToString("000,000,000");
                    //label1.Refresh();
                    Application.DoEvents();
                }

            }

            label2.Text = (DateTime.Now - Start).TotalSeconds + " Sec.";

            Console.WriteLine("Ready.");
        }

Your Count code running on my development laptop.
upload_2019-8-13_8-57-39.png


But I wouldn't call it a simple loop as you have added a if statement to it...
 
Top