Page 10 of 12 FirstFirst ... 56789101112 LastLast
Results 91 to 100 of 113

Thread: Counting

  1. #91
    Resident EE mtekk's Avatar
    Join Date
    Dec 2007
    Location
    Minnesota
    Posts
    469

    Default Re: Counting

    Quote Originally Posted by Trace View Post
    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.

    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;
    }
    Quote Originally Posted by xRyokenx View Post
    ...I'm getting tired of not being able to figure this crap out because it's apparently made for computer-illiterate people by computer-illiterate people. lol

  2. #92
    Water Cooled silverdemon's Avatar
    Join Date
    Jun 2006
    Location
    Delft, Netherlands
    Posts
    520

    Default Re: Counting

    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)

    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
    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)

    enjoy

  3. #93
    You seem to be stood on my tail... Helix666's Avatar
    Join Date
    Jun 2007
    Location
    England, UK
    Posts
    449

    Default Re: Counting

    Quote Originally Posted by XcOM View Post
    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.
    *blink**blink*
    what?

    my last revision outputs the time it takes to run to stdout... and I have the sourcecode to prove it. (well, it's at home, but...)
    Quote Originally Posted by gntlkilr
    warranties are meant to be voided.
    Turns out, they'll let any idiot with a wrench work on hydraulics.

  4. #94
    Resident EE mtekk's Avatar
    Join Date
    Dec 2007
    Location
    Minnesota
    Posts
    469

    Default Re: Counting

    Quote Originally Posted by silverdemon View Post
    ok, here is the snippet of code that does the counting in my VB6 program...
    ...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)

    enjoy
    Looks like VB6's arrays are faster on small numbers than C++ vectors (vector class objects) and C dynamically allocated arrays (using malloc()). That is interesting.
    Quote Originally Posted by xRyokenx View Post
    ...I'm getting tired of not being able to figure this crap out because it's apparently made for computer-illiterate people by computer-illiterate people. lol

  5. #95
    Ceann na Drochaide Bige! XcOM's Avatar
    Join Date
    Mar 2006
    Location
    Sheffield (UK)
    Posts
    2,990

    Default Re: Counting

    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


    Mary had a little lamb. It bumped into a pylon. Ten thousand volts went up its arse and turned its wool to nylon!

  6. #96
    You seem to be stood on my tail... Helix666's Avatar
    Join Date
    Jun 2007
    Location
    England, UK
    Posts
    449

    Default Re: Counting

    XcOM: you did run it from a terminal/command prompt, right?

    Code:
    #include 
    #include 
    #include 
    
    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;
    }
    not pretty, but works. ah well, nevermind.
    Quote Originally Posted by gntlkilr
    warranties are meant to be voided.
    Turns out, they'll let any idiot with a wrench work on hydraulics.

  7. #97
    Resident EE mtekk's Avatar
    Join Date
    Dec 2007
    Location
    Minnesota
    Posts
    469

    Default Re: Counting

    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.
    Quote Originally Posted by xRyokenx View Post
    ...I'm getting tired of not being able to figure this crap out because it's apparently made for computer-illiterate people by computer-illiterate people. lol

  8. #98
    Overclocking Guru Trace's Avatar
    Join Date
    Aug 2007
    Location
    California
    Posts
    2,077

    Default Re: Counting

    Well, apparently the Java compiler didn't lol.
    And on the loops, I think that you would find a:

    Code:
    For x = 1 to CountTo
    'Loop
    Next x
    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-bits
    Quote Originally Posted by Lothair View Post
    I guess it's just widely used and has had some of the best people in the world work on it, costing a ridiculous amount of money, for no actual reason. :/
    Have you checked out the front page lately?
    Projects:
    Moe's Tavern | Sponsored by: Mimo Monitors, Crucial, Thermaltake
    Book Of Knowledge

  9. #99
    Water Cooled silverdemon's Avatar
    Join Date
    Jun 2006
    Location
    Delft, Netherlands
    Posts
    520

    Default Re: Counting

    Quote Originally Posted by mtekk View Post
    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...
    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

  10. #100
    Resident EE mtekk's Avatar
    Join Date
    Dec 2007
    Location
    Minnesota
    Posts
    469

    Default Re: Counting

    Quote Originally Posted by silverdemon View Post
    ...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...
    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:

    Code:
    while(count--);
    would produce more or less the same assembly as:
    Code:
    while(count)
    {
    count = count - 1;
    }
    or
    Code:
    while(count)
    {
    count -= 1;
    }
    or
    Code:
    while(count)
    {
    count--;
    }
    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.
    Quote Originally Posted by xRyokenx View Post
    ...I'm getting tired of not being able to figure this crap out because it's apparently made for computer-illiterate people by computer-illiterate people. lol

Posting Permissions

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