Well, I tried translating your code to C# and I got 218ms (on a Celeron 1.7Ghz) when filling the ListBox and less than 100ns when not filling (ie too small a value for TimeSpan), so the time is spent on filling the ListBox, which I don't think you can speed up.
I had to make a couple of adjustments to your code, so it may not be correct. I also had to guess the contents of StopWatch, but it shouldn't affect speed.
Any C# people may want to try to optimize it (or spot errors in the translation).
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.SuspendLayout();
short i = 3;
short intCounter = 2;
short intDivider;
short intRunner;
bool boolIsPrime = false;
object[] arrPrimes = new object[1000];
arrPrimes[0] = 2;
arrPrimes[1] = 3;
StopWatch sw = new StopWatch();
while(intCounter < 1000)
{
intDivider = (short)Math.Ceiling(Math.Sqrt(i));
if(intDivider > 1)
{
for(intRunner = 3; intRunner <= intDivider; intRunner += 2)
{
if((i % intRunner) == 0)
{
boolIsPrime = false;
break;
}
boolIsPrime = true;
}
if(boolIsPrime)
{
arrPrimes[intCounter] = i;
intCounter++;
}
}
i += 2;
}
listBox1.BeginUpdate();
listBox1.Items.AddRange(arrPrimes);
listBox1.EndUpdate();
label1.Text = sw.Peek().ToString();
this.ResumeLayout();
}
}
class StopWatch
{
private DateTime start;
public StopWatch()
{
start = DateTime.Now;
}
public double Peek()
{
TimeSpan s = DateTime.Now - start;
return (double)(s.Ticks) / 10000;
}
}