473,379 Members | 1,245 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Performance Breakdown Writing to Memory Location

Hi Folks!

For some reasons, this code:
float tmp = 0.;
for (int i = 0; i < 2000; i++) {
for (int j = i; j < 2000; j++) {
for (int k = 0; k < 9000; k++) {
tmp += tmp_mat(k,i) * tmp_mat(k,j);
}
tmp = 0.;

}
cout << "J: " << i << endl;
}
is 10-100 times faster than this

float tmp = 0.;
for (int i = 0; i < 2000; i++) {
for (int j = i; j < 2000; j++) {
for (int k = 0; k < 9000; k++) {
tmp += tmp_mat(k,i) * tmp_mat(k,j);
}
data2d[i][j] = tmp; // the problem
tmp = 0.;

}
cout << "J: " << i << endl;
}


Why is assigning a value that slow?

can anybody help me?

Thanks in advance for your efforts

-Chucker
Nov 17 '05 #1
3 1039
Chucker wrote:
Hi Folks!

For some reasons, this code:
float tmp = 0.;
for (int i = 0; i < 2000; i++) {
for (int j = i; j < 2000; j++) {
for (int k = 0; k < 9000; k++) {
tmp += tmp_mat(k,i) * tmp_mat(k,j);
}
tmp = 0.;

}
cout << "J: " << i << endl;
}


is 10-100 times faster than this

float tmp = 0.;
for (int i = 0; i < 2000; i++) {
for (int j = i; j < 2000; j++) {
for (int k = 0; k < 9000; k++) {
tmp += tmp_mat(k,i) * tmp_mat(k,j);
}
data2d[i][j] = tmp; // the problem
tmp = 0.;

}
cout << "J: " << i << endl;
}


Why is assigning a value that slow?

What is tmp_mat? what's data2d?

First off, I hope you're measuring a release build as debug build timings
are pretty close to meaningless.

Secondly, in the first loop, tmp is probably stored in a floating point
register for the entire operation, while the second form has to make an
additional 4,000,000 memory writes. That's got to take a bit of time.

-cd
Nov 17 '05 #2
Sorry, maybe I did not make myself clear.

1.) All the "unknown" variables are float types

2.) I know that loop 1 does not write to a memory location. I am looking for
the most performant way to do this.

Thanks

Chucker

"Carl Daniel [VC++ MVP]" wrote:
Chucker wrote:
Hi Folks!

For some reasons, this code:
float tmp = 0.;
for (int i = 0; i < 2000; i++) {
for (int j = i; j < 2000; j++) {
for (int k = 0; k < 9000; k++) {
tmp += tmp_mat(k,i) * tmp_mat(k,j);
}
tmp = 0.;

}
cout << "J: " << i << endl;
}


is 10-100 times faster than this

float tmp = 0.;
for (int i = 0; i < 2000; i++) {
for (int j = i; j < 2000; j++) {
for (int k = 0; k < 9000; k++) {
tmp += tmp_mat(k,i) * tmp_mat(k,j);
}
data2d[i][j] = tmp; // the problem
tmp = 0.;

}
cout << "J: " << i << endl;
}


Why is assigning a value that slow?

What is tmp_mat? what's data2d?

First off, I hope you're measuring a release build as debug build timings
are pretty close to meaningless.

Secondly, in the first loop, tmp is probably stored in a floating point
register for the entire operation, while the second form has to make an
additional 4,000,000 memory writes. That's got to take a bit of time.

-cd

Nov 17 '05 #3
Chucker wrote:
Sorry, maybe I did not make myself clear.

1.) All the "unknown" variables are float types
OK.
2.) I know that loop 1 does not write to a memory location. I am
looking for the most performant way to do this.
You may have already found it. Have you looked at a disassembly of the
first loop (without the writes)? In an optimized build, the compiler may
have simply omitted much (or all) of the loop if it can prove that the only
side-effect of the whole thing is to assign 0.0 to tmp.

-cd

Thanks

Chucker

"Carl Daniel [VC++ MVP]" wrote:
Chucker wrote:
Hi Folks!

For some reasons, this code:

float tmp = 0.;
for (int i = 0; i < 2000; i++) {
for (int j = i; j < 2000; j++) {
for (int k = 0; k < 9000; k++) {
tmp += tmp_mat(k,i) * tmp_mat(k,j);
}
tmp = 0.;

}
cout << "J: " << i << endl;
}

is 10-100 times faster than this
float tmp = 0.;
for (int i = 0; i < 2000; i++) {
for (int j = i; j < 2000; j++) {
for (int k = 0; k < 9000; k++) {
tmp += tmp_mat(k,i) * tmp_mat(k,j);
}
data2d[i][j] = tmp; // the problem
tmp = 0.;

}
cout << "J: " << i << endl;
}

Why is assigning a value that slow?

What is tmp_mat? what's data2d?

First off, I hope you're measuring a release build as debug build
timings are pretty close to meaningless.

Secondly, in the first loop, tmp is probably stored in a floating
point register for the entire operation, while the second form has
to make an additional 4,000,000 memory writes. That's got to take a
bit of time.

-cd

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Brian Patterson | last post by:
I have noticed in the book of words that hasattr works by calling getattr and raising an exception if no such attribute exists. If I need the value in any case, am I better off using getattr...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
13
by: Andrew Au \(Newsgroup\) | last post by:
Hi all, I am switching from Java to C solely for performance, but I wonder are there any coding techniques that can boost performance in C? I am asking such an open-ended question to elicit...
5
by: Arthur Mnev | last post by:
This is probably beaten to death subject... Does anyone have a good idea of what penalties are for using Fixed statement in c#. On one side it allows for much greater flexibility with casts and...
4
by: Nadav | last post by:
Hi, I hope this post will find it's way to some MS technical authority... I am experienced with bought Unmanaged C++ and Managed code ( C# ), There are some issues with the .NET framework that...
0
by: BuddyWork | last post by:
Hello, I want to know if there any good tools out there which will show me a breakdown of the memory allocation in Gen 2 heap, basically a breakdown by object is what I'm looking for. The...
13
by: bjarne | last post by:
Willy Denoyette wrote; > ... it > was not the intention of StrousTrup to the achieve the level of efficiency > of C when he invented C++, ... Ahmmm. It was my aim to match the performance...
6
by: Mike | last post by:
Lets just say my app is done HOO HOO. Now, I'm accessing the database via a web service and one thing i noticed that my app is running real slow. When I first started working on the app is ran...
7
by: Michael D. Ober | last post by:
When calling Enqueue, the internal array may need to be reallocated. My question is by how much? In the old MFC array classes, you could tell MFC how many additional elements to add to the array...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.