Can't wait!
Printable View
Can't wait!
And the results are in, honstly im suppurised at them.
And helix, your app didn't have a counter and you didn't reply with a revision that did so, in efect your app does not have any scores becuase i couldn't get any.
its strange i think, VB has 2 out of 5 victorys and Java has 2 out of 5, and C++ has only 1 out of 5.
I was expecting C++ to wipe the floor with everything, but apperently not!
so who won?
Yeah, who won?
I can see that VB (my program:)) wins in the shorter countings, but java (trace) takes over at the larger numbers. Also C++ (Mtekk) is quicker at those numbers.
It seems to me that both Java and C++ count faster, but the program takes a little time to initialize or somthing, since the lower numbers all take about 15-16ms...
Anyways, good job you all!
now: let's see the sources :D
In all reality, there are much faster ways of doing this in C++, but mine was the quickest to write, and kept with the STL. I wrote it in the middle of two weeks filled with midterms. Prince probably used vectors, but probably didn't do any preallocation which decreases performance (mine preallocated before counting). I think the timing method I used isn't necessarily the best either, it's resolution is a little low (has a resolution of 1ms).
I'd like to see the code silverdemon used. Mine is in the bottom of this post.
Just out of curiosity, did you do multiple runs and average them? And what system specs?
Here's my code
Code:/*
John Havlik
4-18-2008
Count Benchmark
*/
#include <iostream>
#include <vector>
#include <fstream>
#include <ctime>
using namespace std;
int main()
{
clock_t start, end, elapsed;
vector<unsigned int> dump;
unsigned char foo;
unsigned int count(1000000), temp(0);
do
{
cout << "How many iterations do you want to do?\n";
cin >> temp;
count = ++temp;
dump.assign(count, 0);
start = clock();
while(--count)
{
dump[count] = count;
};
end = clock();
elapsed = end - start;
cout << "For " << temp << " loops (count from 0 to " << temp - 1 << ")\n We took: " << elapsed << " ms \n Saving...";
ofstream fout;
fout.open("count.out");
for(unsigned int i(0); i < temp; i++)
{
fout << dump[i] << endl;
}
fout.close();
dump.clear();
cout << " Done.\nWould you like to run again?(y/n)\n";
cin >> foo;
}
while(foo == 'y');
return 0;
}
Well, who won?
My code is below.
I entered the second 1.
If you want my VB6 code let me know.(PM)
Java (with output):
Java(without output):Code:import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
class Count2
{
public static void main(String args[]) throws FileNotFoundException
{
Scanner input = new Scanner( System.in );
PrintStream diskWriter = new PrintStream( "D:/java/numbers.txt" );
System.out.print( "Enter number to count to: " );
int countto = input.nextInt();
int count = 0;
long time = System.currentTimeMillis();
while (count < countto)
{
count = count + 1;
diskWriter.println( count );
}
long timeTook = System.currentTimeMillis() - time;
System.out.println("It took you "+timeTook+"ms to count to "+count);
}
}
Code:import java.util.Scanner;
public class Count
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter number to count to: " );
int countto = input.nextInt();
int count = 0;
long time = System.currentTimeMillis();
while ( count < countto)
{
count = count + 1;
}
long timeTook = System.currentTimeMillis() - time;
System.out.println("It took you "+timeTook+"ms to count to "+count);
}
}
Just for fun I opened up Mathematica and did some plots of the times. For the following plots, orange is Trace, blue is me (mtekk), red is Silverdemon, and green is Prince.
Here's Trace's Time vs. Count graph
Here's My Time vs. Count graph
Here's Silverdemon's Time vs. Count graph
Here's Prince's Time vs. Count graph
Here is a combined, graph on the common axis of Prince's plot.
Wait, I think that's the one I entered. I'm not completely sure actually.
If that's the one you entered (the one in the count object, not the count2 object), then doing a similar thing in C++ results in times under the resolution of my timer, for all lengths. :think:
Ok so if you compiler does any optimization you will see your times drop to 0ms if you never use the variable you assign to out of the loop.
If you do use it out side of the loop you get times exactly matching Trace's times (at least on my laptop). What that means is that the Java compiler Trace used did not optimize as it should have (his should have resulted in 0ms for any input number).
The Code I used to test this:
Code:#include <iostream>
#include <ctime>
using namespace std;
int main()
{
clock_t start, end, elapsed;
unsigned char foo;
long count(0), temp(0), temp2;
do
{
cout << "How many iterations do you want to do?\n";
cin >> temp;
count = ++temp;
start = clock();
while(count)
{
temp2 = count--;
}
end = clock();
elapsed = end - start;
cout << "For " << temp2 << " loops (count from 0 to " << temp - 1 << ")\n We took: " << elapsed << " ms\n"
<< "Would you like to run again?(y/n)\n";
cin >> foo;
}
while(foo == 'y');
return 0;
}
ok, here is the snippet of code that does the counting in my VB6 program. There are also comments at the end of all lines (did that for XcOM so he could see what I was doing)
as you can see the numbers are stored in an array. My program also had a display-button, that let's you display the numbers in the array (code not in here)Code:Private Sub cmdStart_Click() 'start button
lblStatus.BackColor = RGB(255, 0, 0) 'make statuslabel red
lblStatus.Caption = "Counting..." 'print busy text
DoEvents 'give windows time to do things (writing in the label)
x = 0 'set startpoint to 0
Finish = Val(txtFinish.Text) 'read endpoint from textbox
ReDim Numbers(1 To Finish) As Long 'ready the array for input
StartTime = timeGetTime 'read the time (starttime)
Do Until x = Finish 'count until x = finish (the number from the textbox)
x = x + 1 'add one to x
Numbers(x) = x 'store x in the array
Loop 'loop the counting
EndTime = timeGetTime 'when finished, get time again (endtime)
TimeTaken = EndTime - StartTime 'calculate time taken
lblStatus.BackColor = RGB(0, 255, 0) 'make statuslabel green and
lblStatus.Caption = "Done!, Numbers stored in array, idle" 'print done text
lblOutput.Caption = Finish & ": " & TimeTaken & "ms" 'print the result to the label
End Sub 'end counting sub
enjoy
i think the winners should be joint winners between
Silverdemon and Trace,
and sorry helix, but when run yourapp it never displayed the output for me.
my app when i factored it in never won a single catagory.
also yes i ran each test 5 times and got an adverage,
the specs were:
AMD X2 Tk55 (1.8GHz Dual core)
1Gb RAM
80GB SATA HDD
XcOM: you did run it from a terminal/command prompt, right?
not pretty, but works. ah well, nevermind.Code:#include <fstream>
#include <iostream>
#include <time.h>
using namespace std;
int main(int argc, char *argv[]){
int count;
time_t timeAtStart, timeAtEnd;
if(argc>1)
count = atoi(argv[1]);
else
count=1e9;
unsigned long int ulNumber=0;
time( &timeAtStart );
cout << "Time at start: " << timeAtStart << endl;
ofstream fout("out.txt");
for(ulNumber=1;ulNumber<(count+1);ulNumber++){
fout << ulNumber << "\n";
}
fout.close();
time( &timeAtEnd);
cout << "Time at end: " << timeAtEnd << endl;
cout << "Time Taken: " << timeAtEnd - timeAtStart << " seconds." << endl;
return 0;
}
Helix, you realize that writing directly to that file is going to really slow things down right? ;) A semi-faster way is to use a stream buffer, dump the numbers into it, and then at the end dump it to the file buffer. The fastest way is to use a vector or array to hold your data. And dump that to the file after you're done with the timed loop, both Silverdemon and I did that. Or you could do what trace did and not actually store the individual values. In which any good compiler will optimize the loop out of the assembly code. :rolleyes:
Well, apparently the Java compiler didn't lol.
And on the loops, I think that you would find a:
Would work better because it doesn't have to check the value everytime, because it is set to run a specific number of times. My 2-bitsCode:For x = 1 to CountTo
'Loop
Next x
That's true. I wanted to make the program as fast as I could, so I got every bit of code outside of the loop. Only the real counting takes place inside the loop. This way the loop will be smaller and since it'll be looped like 1mil or 1bil times every line of code and even every character on the line inside the loop will take a lot of time in the eventual program.
Trace: congrats on the win! +rep is on the way
You mean every instruction, characters are 'free', the number of instructions is what is important.
Trace,
Using a for statement is no faster than using any other loop, the condition to keep within the loop is always checked (unless the loop is unrolled by the compiler). Using something like:
would produce more or less the same assembly as:Code:while(count--);
orCode:while(count)
{
count = count - 1;
}
orCode:while(count)
{
count -= 1;
}
meaning they all will take the same amount of time to complete, unless you use an optimizing compiler that will say, well count just ends up going to zero, so let's skip the loop and just set it to zero.Code:while(count)
{
count--;
}
Oops, my mistake on the loop.
yeah, what I meant was that you want to keep the loop for counting as small as possible. At first I had another line of code in my loop, I believe that was for displaying the number 'líve'. I took a heck of a lot more time then... So like you said, as little as possible instructions in the loop.
btw has anybody started with a refined, better, faster program?
Without devolving into assembly, I'm not exactly sure how to make things any faster. I have implemented Trace's method in C++, which takes 0ms for any number to complete for reasons discussed earlier. :rolleyes: There may be slightly faster libraries for C++ dynamic arrays, the boost libraries come to mind. wxWidgets supposedly have a faster dynamic array as well. I guess I could see if they make any difference. Really, what I'd like to see Trace modify his java to store the values (preferably in an array/vector) and see how fast that is.
I knew full well that my program wouldn't win, but I wanted to be able to see how a basic counting vector versus an optimized one would run, and that right there shows you the difference.
mtekk, I'll see what I can do in the way of the java.
Idea: Let's get teams together of the same language and come up with a new challenege to see which language and team is the best.
We could always play Perl golf, or something similar with a different language.
Or my favorite, obfuscation.
I'll come up with some ideas and post a poll in the chatterbox for you programmers to vote on.
i was thinking of making this a monthly thing, every month or 2 months or 3 months or so,
Set a new challange for coders to write a program and then run them on a test platform to see who wins, there may even be a long one with teams for prizes.
Ooooo.... I like the sound of that.
yeah, me too. I like to program a few simple things every once in a while...
_count_ me in (pun intended)
Ok, I finally got around to re-writing the Java program. The times are almost identical to what they were earlier, crenn.
But I am getting an out of memory error. When I go higher then 10,000,000 so far.
I'm not the best at the optimization, but I can solve some problems pretty fast. Came in second in a programming contest for all high school students in the state and won a Wii.:)