poor performance with prime testing

same code of vb6,vc2010,and with xojo trial.

sub find_prime(max as int32,count as int32)
#pragma BackgroundTasks False
Dim i As int32, j As Int32, k As Integer, prime_count as double
dim t as double
t = Microseconds
For i = i To count
prime_count = 0
For j = 1 To max - 1
For k = 2 To j - 1
If j Mod k = 0 Then
Exit For
End If
If k = j - 1 Then
prime_count = prime_count + 1
End If
Next k
Next j
Next i
t = (microseconds - t)/1000000
dim s as string
s= “waste” + str( t ) + “seconds” + str(prime_count) +“complete”
msgbox s
end sub

find_prime(100000,1)
vc takes 1seconds
vb takes 3seconds
and xo takes 60seconds.

find_prime(1000000,1)
vc takes 120seconds
vb takes 180seconds
and xojo takes more than 30mintus

any wrong?

You’re running it in the debugger then ?
You cannot properly test speed by “running” directly from the IDE.
The app being debugged has a lot of debugging support code built into it that will definitely slow it down.
You’d need to build it and run the executable.

Here on OS X this takes somewhere around 8.3 seconds in a built 32 bit app (I’ve had runs as low as 7.8 and as high as 8.6)
I added a few pragmas since they make the languages more similar in what they do
#pragma BoundsChecking false
#pragma BackgroundTasks False
#Pragma NilObjectChecking False
#Pragma StackOverflowChecking False

the below is c code but takes 120seconds when the count is 10000000 with release optimze.
are you sure only takes 8seconds?? my computer win7 /64bit/ 16GBmemory and cpu 3.1GHZ dualcore. not bad I think.
void Find_Prime(int max,int count)
{
int i=0,j=0,k=0,prime_count=0,time_begin=0,time_end=0;
time_begin=GetTickCount();
for(i=0;i<count;i++)
{
prime_count=0;
for(j=1;j<max;j++)
{
for(k=2;k<j;k++)
{
if(j%k==0)
{
break;
}
if(k==j-1)
{
prime_count++;
}
}
}
}
time_end=GetTickCount();
wsprintf(buffer,“waste %d micro seconds?result is ?%d”,time_end-time_begin,prime_count);
MessageBox(g_hdlg,buffer,"",MB_OK);
}

[quote=270705:@credis c]
For i = i To count
any wrong?[/quote]

That line strikes me as really wrong

[quote=270709:@credis c]
are you sure only takes 8seconds?[/quote]
Yes really only 8 seconds
OS X 10.11.4, 2.7 GHz i7, 16Gb RAM
Numer of cores doesn’t matter since Xojo only uses one

well,that line I cound comment and the same result.
and in c code,that line will just ignore by auto optimize.

I think that one line causes problems
I changed it to For i = 1 To count
I now get 2.9 seconds

from the c code that line should be

  for i = 0 to count - 1

And I now consistently get 3.0 seconds

and what’s the result on the debug model?

you cant speed testing in debug mode. its not useful for real world benchmarks.

i’m testing a compiled app

ok,i still in trial model.

I had only a cursory look through the code, but it appears that you could speed it up considerably by having the inner loop terminating when it reaches the square root of the number that you are testing for prime.

I just comment ''For i = 0 To count
and “end for”
and result same

32 bit builds
find_prime(100000,1) - consistently around 3.3 - 3.5 seconds
find_prime(1000000,1) - 280 (this one I dont understand)

64 bit builds
find_prime(100000,1) - consistently around 7.8 - 8.2 seconds
find_prime(1000000,1) - 716 (this one I dont understand)

I’m using this code (its VERY close to your original)

 Sub find_prime(max as Integer,count as Integer)

  // note that INTEGER automatically is Int32 in 32 bit builds & INT64 in 64 bit builds
  #pragma BoundsChecking false
  #pragma BackgroundTasks False
  #Pragma NilObjectChecking False
  #Pragma StackOverflowChecking False
  
  Dim i As Integer, j As Integer, k As Integer, prime_count as integer
  dim t as double

  t = Microseconds
  For i = 0 To count-1
    prime_count = 0
    For j = 1 To max - 1
      For k = 2 To j - 1
        If j Mod k = 0 Then
          Exit For k
        End If
        If k = j - 1 Then
          prime_count = prime_count + 1
        End If
      Next k
    Next j
  Next i
  
  t = (microseconds - t)/1000000
  
  dim s as string
  s= "waste " + str( t ) + " seconds " + str(prime_count) +" complete "
  
  msgbox s
  
End Sub

In C

#include <iostream>
void Find_Prime(int max,int count) ;

int main(int argc, const char * argv[]) {
	// insert code here...
	std::cout << "Hello, World!\
";
	
	Find_Prime(100000,1) ;
	
    return 0;
}

void Find_Prime(int max,int count)
{
	int i=0,j=0,k=0,prime_count=0;
	clock_t time_begin = std::clock();
	for(i=0;i<count;i++)
	{
		prime_count=0;
		for(j=1;j<max;j++)
		{
			for(k=2;k<j;k++)
			{
				if(j%k==0)
				{
					break;
				}
				if(k==j-1)
				{
					prime_count++;
				}
			}
		}
	}
	clock_t time_end = std::clock();

	std::cout << "waste " << (time_end - time_begin) / CLOCKS_PER_SEC << " seconds?result is "  << prime_count ;

}

Find_Prime(100000,1) = 2 seconds
Find_Prime(1000000,1) = 194 seconds

[quote=270722:@credis c]I just comment ''For i = 0 To count
and “end for”
and result same[/quote]

not quite
i = 0 when started and count = 1 so this loop will run 2 times (0, 1)
Commenting it out changes the test by 1 loop iteration
The code change I suggested makes it more like the C code

Using Norman’s code in my MacBook Air (1.6GHz Core i5)

find_prime(100000,1)

Debug mode, 32-bit: waste 23.03622 seconds 9591 complete
Compiled app, 32-bit: waste 3.523973 seconds 9591 complete

I think the point is more “the same vb code runs faster in vb”
Why ?
There are differences in the languages.
IF I recall correctly VB didn’t have stack checks, out of bounds etc enabled by default.
Xojo does and there is a performance penalty for this safety.
When you turn off those checks things are closer to the same for 32 bit builds
I’m not sure why the 64 bit builds would be so vastly different or why the larger values are so way out of scale.
Thats a puzzler and maybe one I’ll have to put to our compiler guy - he might have some insight.

[quote=270726:@Tiago Ribeiro]Using Norman’s code in my MacBook Air (1.6GHz Core i5)

find_prime(100000,1)

Debug mode, 32-bit: waste 23.03622 seconds 9591 complete
Compiled app, 32-bit: waste 3.523973 seconds 9591 complete[/quote]

Respectable for such a slow machine :stuck_out_tongue:

sorry I know there a lot diffence with vb,vc and xojo.
and I just compared the simple syntax like “for,end for if ,else” for performance.

I ofcourse not doubt that xojo is better than vb nowadays.
seems the problems here is something is wrong with 32/64bit?