COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Question of the day!!!

• Email
• Follow

```Code A:
float a=5 ,b=10;
float c=a+(b-a)/a+(b-a)*(b-a);
//End of code A

Code B:
float a=5,b=10;
float d=b-a;
float c=a+d/a+d*d;

Question:

Which code segment A or B takes more memory and which ones take more
memory?

```
 0
Reply email.anshum (6) 10/1/2011 1:04:41 PM

See related articles to this posting

```Anshum Kocher wrote:
) Code A:
) float a=5 ,b=10;
) float c=a+(b-a)/a+(b-a)*(b-a);
) //End of code A
)
) Code B:
) float a=5,b=10;
) float d=b-a;
) float c=a+d/a+d*d;
)
) Question:
)
) Which code segment A or B takes more memory and which ones take more
) memory?

Your question doesn't make sense.  Even ignoring the poor wording.
In any case, any decent compiler will generate the same code for both,
assuming 'd' isn't used somewhere else.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
```
 0
Reply willem6 (255) 10/1/2011 1:12:28 PM

```On 10/01/2011 09:04 AM, Anshum Kocher wrote:
> Code A:
> float a=5 ,b=10;
> float c=a+(b-a)/a+(b-a)*(b-a);
> //End of code A
>
> Code B:
> float a=5,b=10;
> float d=b-a;
> float c=a+d/a+d*d;
>
> Question:
>
> Which code segment A or B takes more memory and which ones take more
> memory?

That depends entirely upon the compiler and the context. Assuming that
the above is the only difference between two otherwise identical
programs, most decent compilers should generate essentially equivalent
code for both cases.
--
James Kuyper
```
 0
Reply jameskuyper (5635) 10/1/2011 2:41:24 PM

```On Oct 1, 2:04=A0pm, Anshum Kocher <email.ans...@gmail.com> wrote:
> Code A:
> float a=3D5 ,b=3D10;
> float c=3Da+(b-a)/a+(b-a)*(b-a);
> //End of code A
>
> Code B:
> float a=3D5,b=3D10;
> float d=3Db-a;
> float c=3Da+d/a+d*d;
>
> Question:
>
> Which code segment A or B takes more memory and which ones take more
> memory?

you do know you asked the same question twice? Why don't you just
measure it, if you care. I suspect the answer is "not much" or "none
at all". For the record I'd use the second version as I don't like
repeating myself. Oh and I'd prefer double to float.
```
 0
Reply nick_keighley_nospam (4575) 10/2/2011 10:01:02 AM

```On Oct 2, 3:01=A0pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
> On Oct 1, 2:04=A0pm, Anshum Kocher <email.ans...@gmail.com> wrote:
>
> > Code A:
> > float a=3D5 ,b=3D10;
> > float c=3Da+(b-a)/a+(b-a)*(b-a);
> > //End of code A
>
> > Code B:
> > float a=3D5,b=3D10;
> > float d=3Db-a;
> > float c=3Da+d/a+d*d;
>
> > Question:
>
> > Which code segment A or B takes more memory and which ones take more
> > memory?
>
> you do know you asked the same question twice? Why don't you just
> measure it, if you care. I suspect the answer is "not much" or "none
> at all". For the record I'd use the second version as I don't like
> repeating myself. Oh and I'd prefer double to float.

It was more of a typing error....the actual question was
Which code segment A or B takes more memory and which ones take more
time?
and it was a question asked to me in a written examination...
```
 0
Reply email.anshum (6) 10/3/2011 11:17:59 AM

```On 10/3/2011 7:17 AM, Anshum Kocher wrote:
>[...]
> It was more of a typing error....the actual question was
> Which code segment A or B takes more memory and which ones take more
> time?
> and it was a question asked to me in a written examination...

If the written examination concerned the C language, the
correct answers would have been

"It depends on the implementation."
and
"It depends on the implementation."

If the examination concerned a specific implementation of C,
the correct answers would have been

"It depends on the context."
and
"It depends on the context."

If the examination concerned a specific implementation of C
in the context of a specific complete program, the correct answers
would have been

"I'll have to measure it."
and
"Who cares?"

Seriously.

--
Eric Sosman
esosman@ieee-dot-org.invalid
```
 0
Reply esosman2 (3096) 10/3/2011 11:33:02 AM

```> Code A:
> float a=5 ,b=10;
> float c=a+(b-a)/a+(b-a)*(b-a);
> //End of code A
>
> Code B:
> float a=5,b=10;
> float d=b-a;
> float c=a+d/a+d*d;
>
> Question:
>
> Which code segment A or B takes more memory and which ones take more
> memory?

This is, of course, platform and compiler dependent.

There is a good chance that both generate identical code, using
identical amounts of storage and time, but that B's code is more
identical to A's code than vice versa.

```
 0
Reply gordon13 (233) 10/4/2011 5:11:47 AM

```On Mon, 2011-10-03, Anshum Kocher wrote:
> On Oct 2, 3:01�pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
> wrote:
>> On Oct 1, 2:04�pm, Anshum Kocher <email.ans...@gmail.com> wrote:
>>
>> > Code A:
>> > float a=5 ,b=10;
>> > float c=a+(b-a)/a+(b-a)*(b-a);
>> > //End of code A
>>
>> > Code B:
>> > float a=5,b=10;
>> > float d=b-a;
>> > float c=a+d/a+d*d;
>>
>> > Question:
>>
>> > Which code segment A or B takes more memory and which ones take more
>> > memory?
>>
>> you do know you asked the same question twice? Why don't you just
>> measure it, if you care. I suspect the answer is "not much" or "none
>> at all". For the record I'd use the second version as I don't like
>> repeating myself. Oh and I'd prefer double to float.
>
> It was more of a typing error....the actual question was
> Which code segment A or B takes more memory and which ones take more
> time?
> and it was a question asked to me in a written examination...

Then don't trust whoever wrote that question. Unless it was a trick
question or had additional restrictions, that person doesn't seem to
understand C (or programming in general).

Both A and B can be optimized away to three constants -- or less if
the rest of the code doesn't use all of them.

/Jorgen

--
// Jorgen Grahn <grahn@  Oo  o.   .     .
\X/     snipabacken.se>   O  o   .
```
 0
Reply nntp24 (1801) 10/4/2011 10:21:35 AM

```On Oct 1, 9:04=A0am, Anshum Kocher <email.ans...@gmail.com> wrote:
> Code A:
> float a=3D5 ,b=3D10;
> float c=3Da+(b-a)/a+(b-a)*(b-a);
> //End of code A
>
> Code B:
> float a=3D5,b=3D10;
> float d=3Db-a;
> float c=3Da+d/a+d*d;
>
> Question:
>
> Which code segment A or B takes more memory and which ones take more
> memory?

Independent of architecture and compiler, B takes up more space.
```
 0
Reply henryeshbaugh (24) 10/5/2011 12:48:12 AM

```henry eshbaugh <henryeshbaugh@gmail.com> writes:

> On Oct 1, 9:04 am, Anshum Kocher <email.ans...@gmail.com> wrote:
>> Code A:
>> float a=5 ,b=10;
>> float c=a+(b-a)/a+(b-a)*(b-a);
>> //End of code A
>>
>> Code B:
>> float a=5,b=10;
>> float d=b-a;
>> float c=a+d/a+d*d;
>>
>> Question:
>>
>> Which code segment A or B takes more memory and which ones take more
>> memory?
>
> Independent of architecture and compiler, B takes up more space.

Why do you say that?  Part of my question is just that I know what
"takes up more space" means for code fragments like this.

--
Ben.
```
 0
Reply ben.usenet (6790) 10/5/2011 1:08:07 AM

```henry eshbaugh <henryeshbaugh@gmail.com> writes:
> On Oct 1, 9:04 am, Anshum Kocher <email.ans...@gmail.com> wrote:
>> Code A:
>> float a=5 ,b=10;
>> float c=a+(b-a)/a+(b-a)*(b-a);
>> //End of code A
>>
>> Code B:
>> float a=5,b=10;
>> float d=b-a;
>> float c=a+d/a+d*d;
>>
>> Question:
>>
>> Which code segment A or B takes more memory and which ones take more
>> memory?
>
> Independent of architecture and compiler, B takes up more space.

Evidence?

Yes, I know B defines more objects than A does; that won't necessarily
show up in the generated code.

And in fact "gcc -O3" generates *exactly* the same code for both
(with a small wrapper program that prints the value of c).

--
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
```
 0
Reply kst-u (21963) 10/5/2011 1:13:56 AM

```henry eshbaugh wrote:

> Independent of architecture and compiler, B takes up more space.

I count 48 characters for both code fragments.
Ergo, they take the same space.
```
 0
Reply Noob 10/5/2011 12:29:20 PM

11 Replies
53 Views

Similar Articles

12/19/2013 2:53:20 PM
page loaded in 69608 ms. (0)

Similar Artilces:

Question of the day
Hello All, If you should be so happy to have a house that has more than one electrical panel. (ie sub-panels) Can you get away with one signal bridge at the main panel or is it wise to have one on each panel??? Also should you have two 200 amp panels (400amp service) as mains is it suggested to have a signal bridge in each or will one do the job??? Thanks in advance. Les "ABLE_1" <royboynospam@somewhere.net> wrote in message news:5-SdnXBhHb2VEoHYnZ2dnUVZ_oGdnZ2d@comcast.com... > Hello All, > > If you should be so happy to have a house that has more than one...

Excel Question of the day
I have a problem that I'm hoping someone has solved before. I have an Excel template that has a series of graphs on the first page, and points to specific locations on other worksheets in the same Excel file. Can ODS HTML write specific data elements to specific cells on specific workbooks in an Excel template? Thanks. =20 _______________________________ Bruce A. Johnson Senior Data Analyst Solucient, LLC (847) 440-9635 bjohnson@solucient.com=20 This message is a private communication. It may contain information that = is confidential=20 and legally protected from disclosure. If you ar...

question of the day#2
1-10 how much u luv gameskb -- sk8er chick Message posted via GamesKB.com http://www.gameskb.com/Uwe/Forums.aspx/pc-games-rpg/200607/1 ash82695, These discussion groups are not GamesKB specific. GamesKB is just one of the ways to access the groups and many people use different newsreaders or web gateways. ash82695 wrote: >1-10 how much u luv gameskb "ash82695 via GamesKB.com" <u23635@uwe> wrote in message news:62a62153f62c6@uwe... > 1-10 how much u luv gameskb ....never heard of it until your question #1 ash82695 via GamesKB.com wrote: > 1-10 how much u ...

Silly Cisco question of the day
Hi gang, Have a 7206 with a 100Base-FX port on it, and the following DHCP pool config: ip dhcp pool 0 network 10.12.200.0 255.255.255.0 domain-name xxxx dns server 10.1.1.7 default-router 10.12.0.1 lease 30 Doesn't get much simpler, right? OK, now the 100Base-FX port is connected to an old 6006, and in that 6006 is a 48-port 100BT blade. No machine connected to the 6006 can get an address via DHCP. In fact, the 7206 also has a 100BT port, which I have since connected to a 3500, and I can't get DHCP on that port either. Correct me if I am wrong, but DHCP requests are broadc...

SAS Day-Time Conversion Question
Hi Everyone, Do you know of a way to convert a time value that is in the following format: 2d 06:30:20 03:02:35 36d 20:34:26 to a SAS time value? I've been combing the documentation and I've found lots of info about how to convert to HH:MM:SS but nothing that will interpret the days (i.e. "2d") or anything that includes any time period larger than 24 hours. I am tempted to just split the day and time component and multiply the day component by 60*60*24 (the total number of seconds in a day) because this field is from a column-delimited (fixed width) data, but may...

Simple question: how do I do day/month arithmetic?
This is a pretty simple question, and I know there's a way to brute-force it by just giving it a look-up table of the number of days in each month, but I was wondering if there's a more elegant way (perhaps using some Perl calendar library or something?) Anyway, I want to write a Perl script that says how many days are left until a certain date. A sort of count-down timer, if you will. It will be using some sort of system clock call to get the current date (also, how do you do that?) Thanks for the help. I know this is pretty easy stuff, but I'm a beginner at Perl. ...

Question about the 497.1-day-uptime bug.
Hello! In VAX/VMS V6.2, there is the 497.1-day-uptime bug. Does anyone happen to know if V6.1 also has this bug? I looked on the Web but couldn't find anything. A description of the bug follows. Thanks! Problems addressed in VAXSYSA02_062 kit o If a system has been up for 497.1 days without rebooting, the system cell EXE\$GL_ABSTIM_TICS (number of 10 millisecond tics since boot) will overflow. This problem can cause some processes to remain indefinitely in the RWMPB or COMO scheduling state. Furthermore, candidate processes for Virtual Balance Set...

Query Question: How to select all days within a period
Hi all, I have what I think is a rather a curly question: Is there a way to select all days in a month (or week, year, any period) without having matching rows in a table? So in essence, can I create a query that returns all the days in a period without looking up a table? To give some background, I am trying to output records for a graphing/ reporting tool and I only have non-zero records in my table. Any insights, suggestions are warmly welcomed! Thanks. On May 5, 8:37 am, supagroova <supagro...@gmail.com> wrote: > Hi all, > > I have what I think is a rather a curly que...

sw world question on the final day final afternoon...
Hi all, My return flight from LV on wed night has been cancelled!? I asked them to reinstate the flight but havn't heard back yet. Assuming that they wont (!) - I have the option to leave mid-afternoon on wed or wait till thursday with the resultant higher hotel fees, missed work etc. So, question is: In the past has the last afternoon been a case of people packing and rushing to get home or cramming as much as possible into a 3day conference? Thanks, Zander Last year I attended a class in the last session that was on weldments, and the class was packed. The presenter was somewha...

Re: SAS Day-Time Conversion Question #2
As far as I know there is no INFORMAT for that purpose. You can do as you suggest convert DAYS to seconds and add to time. DHMS can be used for this purpose. 81 data _null_; 82 infile cards dlm='d '; 83 input days time:time8.; 84 duration = dhms(max(0,days),0,0,time); 85 format time time8. duration time10.; 86 put (_all_)(=); 87 cards; days=2 time=6:30:20 duration=54:30:20 days=. time=3:02:35 duration=3:02:35 days=36 time=20:34:26 duration=884:34:26 On Nov 7, 2007 10:42 AM, Florio Arguillas <foa2@cornell.edu> wrote: > Hi Everyone, > >...