Ahmedn1's Profile User Rating: -----

Reputation: 23 Tradesman
Group:
Contributors
Active Posts:
558 (0.28 per day)
Joined:
04-August 09
Profile Views:
19,869
Last Active:
User is offline Dec 14 2014 06:43 AM
Currently:
Offline

Previous Fields

Country:
EG
OS Preference:
Windows
Favorite Browser:
FireFox
Favorite Processor:
Intel
Favorite Gaming Platform:
XBox
Your Car:
Who Cares
Dream Kudos:
125

Latest Visitors

Icon   Ahmedn1 has not set their status

Posts I've Made

  1. In Topic: Weird Loop behaviour in C++ (needs explanation)

    Posted 10 Dec 2014

    View Postjimblumberg, on 10 December 2014 - 11:46 PM, said:

    int main(int argc, char** argv)
    {
    
       cout.sync_with_stdio(false);
    /*
      for(int i = 0; i < 100; i++)
            for(int j = 0; j < 100; j++)
                for(int k = 0; k < 10; k++)
                    cout << i << ' ' << j << ' ' << k << ' ';
    */
       for(int i = 0; i < 100; i++)
            for(int j = 0; j < 100; j++)
                for(int k = 0; k < 10; k++)
                    cout << i << ' ' << j << ' ' << k << endl;
    
       for(int i = 0; i < 100000; i++)
          cout << i << endl;
    
       for(int i = 0; i < 100000; i++)
          cout << i << ' ';
    
       return(0);
    }
    
    


    Jim


    The first set of loops took around 2.0 seconds, the second set of loops took about 31 seconds.

    The fourth loop took about 29 seconds, and the fifth took about 1.556 seconds.

    Edit:
    This is all with no optimization.
    Edit2
    This is also on a 64 bit Linux system, using g++ with no optimization.


    Jim


    Aha I see
    for the sake of fair measurement please add the variables j and k to the single loop as well and see the results then

    I mean like this

    for(int i = 0, k = 0, j = 0; i < 100000; i++, k++, j++)
          cout << i << ' ' << k << ' ' << j << ' ';
    
    
  2. In Topic: Weird Loop behaviour in C++ (needs explanation)

    Posted 10 Dec 2014

    View Postjimblumberg, on 10 December 2014 - 11:01 PM, said:

    You must really be running on an extremely slow system to be getting times as reported. But no matter, you are not actually timing the two loops you are timing the time it takes to print something to the screen and you really don't have near enough iterations to determine which is faster. You need to be in at least the 10's of seconds not tenths of seconds, by the way my machine completed in about .012 seconds. Also you should know that you are determining how fast your console will print the characters and flush the output buffer, adding characters to be printed will increase the run time.

    I took your single loop and ran it for 100,000 iterations at a time of 29.9 seconds, when I removed the endl it took less than 1 second to run the same loop.

    With your multiple loop with 100,100,10 iterations with the endl 31.3 with '\n' instead of endl 30.05 and with ' ' (a space) it took 1.8 seconds.


    Jim


    Thanks for the reply Jim,
    First of all I don't know why it is too slow :) my processor is Intel Core i5-3317U CPU 1.7 GHz with 4 GB RAM
    Anyway, it doesn't matter :)
    But for the point of "you are not actually timing the two loops you are timing the time it takes to print something to the screen", does it matter even if it is the same operation in both loops?
    I mean it may take some time to print and flush as you said but it is the same operation in both loops so whatever time it takes it shouldn't matter. It should give me a clear measure of the loops timing.
    In addition, I actually tested your theory with 100,000 loops and changing the endl with a space
    here is the code

    
        for(int i = 0; i < 100; i++)
            for(int j = 0; j < 100; j++)
                for(int k = 0; k < 10; k++)
                    cout << 1 << j << k << " ";
    
    


        for(int i = 0, k = 0, j = 0; i < 100000; i++, k++, j++)
            cout << 1 << j << k << " ";
    
    


    I ran each of them 3 times and recorded the execution times to be sure of the average case for each one:
    The Nested Loop results:
    24.782 s
    25.202 s
    24.964 s

    The Single Loop results:
    32.408 s
    31.656 s
    31.968 s

    So at least in my side the problem still exists :) Any ideas?

    Can you send me the exact code you wrote so I can test it on my side?
  3. In Topic: Weird Loop behaviour in C++ (needs explanation)

    Posted 10 Dec 2014

    View Postmodi123_1, on 10 December 2014 - 10:09 PM, said:

    Try using the J and K in your other loops.

    I am thinking there may be some compiler optimization done that checks to see if the loop variables are being used and if not drops the loop.


    I tested your theory and it did actually change the execution time remarkably

    No the code is like this

    
        for(int i = 0; i < 10; i++)
            for(int j = 0; j < 10; j++)
                for(int k = 0; k < 10; k++)
                    cout << 1 << j << k << endl;
    
    


    and the Single Code is

    
        for(int i = 0, k = 0, j = 0; i < 1000; i++, k++, j++)
            cout << 1 << j << k << endl;
    
    


    When I ran the Nested code the execution time was between 0.270 and 0.310
    But, when I ran the Single code the time was between 0.360 and 0.500 :)

    So, yes your theory may be right abut optimizing the variables used but it still the single loop is taking too much time compared to the three nested loops
  4. In Topic: Matrix of Buttons Variables

    Posted 15 Jun 2014

    View PostSkydiver, on 15 June 2014 - 07:22 AM, said:

    Please do not give complete solutions to problems. You have enough posts in this forum to know it works: We guide people towards solutions. We don't just spoonfeed them the answer.



    Sorry for that it's been a time since I last posted here so I kinda misreacted
  5. In Topic: Matrix of Buttons Variables

    Posted 14 Jun 2014

    You can simply store the i, j in the buttons Tag property and then get them back again:

    private void Single_Load(object sender, EventArgs e)
            {
                //code here
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        //a lot of working code here
                        b[i, j].Click += new System.EventHandler(ClickButton);
                        b[i, j].Tag = i.ToString() + "," + j.ToString;
                    }
                }
            }
    
    
    


    ClickButton(object sender, EventArgs e)
        {
            string tag = ((Button)sender).Tag.ToString();
            string[] indexes = tag.Split(',');
            int i = int.Parse(indexes[0]);
            int j = int.Parse(indexes[1]);
            if (x%2==0)
                v[i, j] = x; //i and j should be form the matrix of buttons
        }
    
    

My Information

Member Title:
D.I.C Addict
Age:
25 years old
Birthday:
April 17, 1989
Gender:
Location:
Egypt
Interests:
Reading
Computers & Internet
Programming
Travelling
Full Name:
Ahmed Elsaid Mohamed Dawod
Years Programming:
5
Programming Languages:
VB 6
VB.NET
C#
C++
Python
Java SE

Contact Information

E-mail:
Click here to e-mail me
MSN:
MSN  ahmedn12009@hotmail.com
Website URL:
Website URL  http://www.perfectcodes.com
Yahoo:
Yahoo  ahmedn32004
Skype:
Skype  ahmedn12007
Facebook:
Ahmedn1.Dawod
Twitter:
Ahmed_n1

Friends

Comments

Ahmedn1 has no profile comments yet. Why not say hello?