473,883 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

removing a loop cause it to go at half the speed?

Hi

I was doing a simple test of the speed of a "maths" operation and when I
tested it I found that removing the loop that initialises the data array
for the operation caused the whole program to spend twice the time to
complete. If the loop is included it takes about 7.48 seconds to
complete, but when removed it takes about 11.48 seconds.

Does anybody have a suggestion as to why this is so and whether I can
trust the results of the code as it is below?

/tom
The code was compiled on linux 2.6.3 with gcc 3.3.2 and glibc 2.2.3
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

int main(int argc, char *argv[])
{
int total = 0;
int count = 65500;
signed int data[count];

/* Initialising array loop */
for(int c=0; c<count; c++) {
data[c]=1+(int) (2000000000.0*r and()/(RAND_MAX+1.0)) ;
}

struct timeval start_time;
struct timeval end_time;

gettimeofday(&s tart_time, NULL);

for(int d=0; d<50000; d++) {
for(int c=0; c<count; c++) {
total += data[c];
}
}
gettimeofday(&e nd_time, NULL);

double t1=(start_time. tv_sec*1000)+(s tart_time.tv_us ec/1000.0);
double t2=(end_time.tv _sec*1000)+(end _time.tv_usec/1000.0);

printf("Elapsed time (ms): %.6lf\n", t2-t1);
printf("Total: %u\n", total);

return(0);
}
Mar 15 '06
102 4590
On Thu, 16 Mar 2006 09:35:24 +0100, in comp.lang.c , tom fredriksen
<to*@nospam.org > wrote:
:) sorry but using an uninitialised array does not cause undefined
behaviour.
Yes, it does. Quoting from the ISO Standard:
J.2 Undefined behavior
1 The behavior is undefined in the following circumstances:
- The value of an object with automatic storage duration is used while
it is indeterminate (6.2.4, 6.7.8, 6.8).
It only means the values of the array are not preset.
This is known as being "indetermin ate"
An
array is just a sequence of memory positions, which have been used
previously by some other part of the program or the system, hence the
value it has is what was there before I allocated it.
And that value may be a trap representation, or not a number at all.
However its totally irrelevant since you are expressly forbidden from
doing this by hte ISO Standard.
If I had used the value in some expression,


You did - you read them.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 16 '06 #51
On Thu, 16 Mar 2006 11:15:15 +0100, in comp.lang.c , tom fredriksen
<to*@nospam.org > wrote:
Wrong, it does not cause undefined behaviour because the values used
does not require a specific set of values or a value range.
This is utterly irrelevant. The ISO standard expressly forbids you to
read uninitialised memory.
its just a
set of ints with a 32 bit value, nothing special about that. Its the
interpretati on that matters not the value itself. And since my program
does not need to interpret the value there is no undefined behaviour.
It does, since it reads the value.
The reason it is stated that it causes undefined behaviour is because
the programmer has to decide what behaviour it should have in those
cases.


No!!! The programmer has no role, undefined behaviour is behaviour
that the ISO Standard says is undefined.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 16 '06 #52
Rod Pemberton said:

"Richard Heathfield" <in*****@invali d.invalid> wrote in message
news:dv******** **@nwrdmz03.dmz .ncs.ea.ibs-infra.bt.com...

Actually, I think Tom's interpretation for his implentation is correct.


His implementation is neither here nor there. The behaviour of the code is
undefined as far as the C language is concerned.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 16 '06 #53
On Thu, 16 Mar 2006 13:51:23 +0100, in comp.lang.c , tom fredriksen
<to*@nospam.org > wrote:
Can you then explain to me how it is that you think the behaviour of my
code can not be predicted?
One of the values may be a trap representation, upon reading which
your operating system is obliged to abort the programme. Since you
don't know whats in the memory, you can't predict your programme's
behaviour and it will randomly crash in an unexplicable fashion.

I guarantee you that it will do it when you are trying to demonstrate
to a customer.
Are you actually telling me that just because
the array is uninitialised my program will fail/have undefined
behaviour, independent of how I use the array? lol:)
Yes. The only way you can use the array is by initialising it.
Before you answer, you should know that the reason for the problem is
that the rand() statement causes the compiler to output float operations
instead of integer operations.


Whats this got to do with the price of fish? Just because you found
another bug, doesn't mean this one goes away.

"Officer, I hear you telling me its illegal to run a red light, but
now I'm keeping to the speed limit, your argument abour red lights has
exploded in its face. So there"
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 16 '06 #54
On Thu, 16 Mar 2006 14:26:14 +0100, in comp.lang.c , tom fredriksen
<to*@nospam.org > wrote:
I think there is no point continuing the discussion.
There certainly isn't. You evidently aren't prepared to learn even
from some of the most experienced C programmers around.
But if you consider the pragmatics of it, in addition to the
literal meaning, which you should when you are programming, then you are
wrong.


No, he's still right, we're still right.

However you won't see that because you're totally convinced that your
own tiny corner of the galaxy is the only inhabitable world and life
*cannot* exist elsewhere.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 16 '06 #55
On Thu, 16 Mar 2006 17:17:14 +0100, in comp.lang.c , tom fredriksen
<to*@nospam.org > wrote:
No I don't think that. But undefined behaviour in my book is behaviour
not defined, if you tell the computer to do something then its defined.
Bullshit. Lets say you tell the computer to write to byte data[c] on
your hard disk. Is that a defined operation? No.
The compiler controls the undefined behaviour so, therefore its not
undefined anymore, portability or not.
Absolute caeculum tauri.
Lets just agree that we to some extent disagree,


The only thing we can agree on is that you are too arrogant to be
taught, it seems.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 16 '06 #56
On Thu, 16 Mar 2006 17:20:35 +0100, in comp.lang.c , tom fredriksen
<to*@nospam.org > wrote:
The undefined behaviour here is if an overflow occurs and there is no
mechanism to handle that overflow, what happens with the OF bit then?
what OF bit? Who says there is one?
In my program that was of no concern, so the "undefined behaviour" is
irrelevant.


So if the setting of the OF bit causes your processor to abort your
programme, thats of no concern? You really /are/ a chump.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 16 '06 #57
On Thu, 16 Mar 2006 14:12:31 -0500, in comp.lang.c , "Rod Pemberton"
<do*********@so rry.bitbuck.cmm > wrote:
Actually, I think Tom's interpretation for his implentation is correct.
Take another look:
C89 draft:

* Undefined behavior --- behavior, upon use of a nonportable or
erroneous program construct, of erroneous data,
The construct and data aren't errnoneous for his implementation. But, may
be elsewhere.


The words "for your implementation" do not appear in your quote.
or of
indeterminately-valued objects,


They aren't indeterminately-valued for his implementation.


Again, irrelevant.
for which the Standard imposes no
requirements.


Therefore, valid for his implementation. Maybe UB elsewhere.


Doesn't work like that. UB is what is not defined in the standard, not
what is not defined for a specific implementation.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 16 '06 #58
Mark McIntyre <ma**********@s pamcop.net> writes:
On Thu, 16 Mar 2006 09:35:24 +0100, in comp.lang.c , tom fredriksen
<to*@nospam.org > wrote:
:) sorry but using an uninitialised array does not cause undefined
behaviour.


Yes, it does. Quoting from the ISO Standard:
J.2 Undefined behavior
1 The behavior is undefined in the following circumstances:
- The value of an object with automatic storage duration is used while
it is indeterminate (6.2.4, 6.7.8, 6.8).


Now this is interesting. Appendix J is informative, not normative,
so the fact that accessing an indeterminate object invokes undefined
behavior should be stated normatively elsewhere -- but I can't find it.

Sections 6.2.4 ("Storage duration of objects"), 6.7.8,
("Initializatio n"), and 6.8 ("Statements and blocks") all say that
says that the initial value of an automatic object is indeterminate,
but they don't say anything about attempting to access that value.

If the statement in J.2 is to be taken literally, then in this
program:

int main(void)
{
unsigned char x;
unsigned char y;
y = x;
return 0;
}

the assignment invokes undefined behavior. Clearly, the value of x is
indeterminate. Since an indeterminate value is either an unspecified
value or a trap representation, and since unsigned char has no trap
representations , it must be an unspecified value. By definition, an
"unspecifie d value" is a "valid value of the relevant type".

What normative text in the standard says that this is undefined
behavior? (There would have to be an explicit statement; the behavior
of the assignment is specified in 6.5.16.1.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 17 '06 #59
On 2006-03-16, Rod Pemberton <do*********@so rry.bitbuck.cmm > wrote:
"David Holland" <dh******@rever se-this--edu.harvard.fas > wrote in message
<OT purpose=stop_wi ld_speculation>
It is because of the virtual memory system. When you initialize the
array beforehand, it forces the virtual memory system to materialize
memory for the pages appearing in the array. When you do not, this
materialization happens in the timed loop, and then of course it's
slower.
[...]


FYI, DJGPP does use a virtual memory system by default. [...]


Huh. Neat. Does it defer materialization of untouched pages until
they're used? I imagine it probably does.

Anyhow, I withdraw this explanation, now that I've read the code more
carefully. Materializing 64 pages is a negligible cost compared to 3
billion or so loop cycles, and I should have noticed this before
opening my mouth. (Likewise, initial cache warmth is irrelevant
because the loop touches the array 50,000 times, of which 49,999 occur
with warm cache either way.)

On my platform I can get a 20% speed improvement by inserting twelve
nop instructions. I'm going to assume the OP's results are purely a
matter of whether the loop code ends up cache-aligned or not.
</OT>


--
- David A. Holland
(the above address works if unscrambled but isn't checked often)
Mar 17 '06 #60

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

Similar topics

24
8655
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I have used. Original code : c= 0 ; for (i=0; i<999; i++)
0
9933
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9781
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10836
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9567
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7960
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4211
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.