Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #23946 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2014-04-25 22:53 -0700 |
| Last post | 2014-04-26 08:06 -0700 |
| Articles | 20 on this page of 21 — 5 participants |
Back to article view | Back to comp.lang.javascript
Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-25 22:53 -0700
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-25 22:57 -0700
Re: Program logic weirdness? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-26 12:35 +0100
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-26 07:59 -0700
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-26 08:11 -0700
Re: Program logic weirdness? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-26 22:34 +0100
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-27 00:44 -0700
Re: Program logic weirdness? Tim Streater <timstreater@greenbee.net> - 2014-04-27 13:20 +0100
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-27 06:09 -0700
Re: Program logic weirdness? Tim Streater <timstreater@greenbee.net> - 2014-04-27 18:30 +0100
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-27 12:13 -0700
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-28 02:33 -0700
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-27 06:25 -0700
Re: Program logic weirdness? Denis McMahon <denismfmcmahon@gmail.com> - 2014-04-27 14:55 +0000
Re: Program logic weirdness? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-27 15:41 +0100
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-27 13:57 -0700
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-25 23:18 -0700
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-25 23:23 -0700
Re: Program logic weirdness? Christoph Michael Becker <cmbecker69@arcor.de> - 2014-04-26 15:06 +0200
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-26 08:00 -0700
Re: Program logic weirdness? jonas.thornvall@gmail.com - 2014-04-26 08:06 -0700
Page 1 of 2 [1] 2 Next page →
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-25 22:53 -0700 |
| Subject | Program logic weirdness? |
| Message-ID | <9642e93e-b03c-4898-a032-14ef38d3cc2e@googlegroups.com> |
I find some things causing logical problems in programming. I have a loop, that will print out two arrays filled with element A and B. Now B have prerequisite that it will only be printout when x<3 dealing with Writing to canvas i want todo so Little operations as possible in the actual printout. And x is set Before we go into loop? Of course i do not want to check for each element, because x is already set when i go into loop. But it also seem weird that i should need to run two legs, e.g two loops one for A and one for B due to a prerequisite on B? I mean let us say there was 50 variables and different prerequisites instead of two? I can hardly run each printout in its own leg? This problem must have a general solution that i have learned but forgotten? How is this generally solved?
[toc] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-25 22:57 -0700 |
| Message-ID | <75140a58-803e-4694-9b49-6d4561736bb8@googlegroups.com> |
| In reply to | #23946 |
Den lördagen den 26:e april 2014 kl. 07:53:59 UTC+2 skrev jonas.t...@gmail.com: > I find some things causing logical problems in programming. > > > > I have a loop, that will print out two arrays filled with element A and B. > > > > Now B have prerequisite that it will only be printout when x<3 dealing with Writing to canvas i want todo so Little operations as possible in the actual printout. And x is set Before we go into loop? > > > > Of course i do not want to check for each element, because x is already set when i go into loop. > > > > > > But it also seem weird that i should need to run two legs, e.g two loops one for A and one for B due to a prerequisite on B? > > > > I mean let us say there was 50 variables and different prerequisites instead of two? I can hardly run each printout in its own leg? > > > > This problem must have a general solution that i have learned but forgotten? > > > > How is this generally solved? To me it seem like a problem above the actual scripting technic more like a problem when constructing a program language. But maybe i am missing something fairly obvious here?
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2014-04-26 12:35 +0100 |
| Message-ID | <0.a3b4f471693338248a20.20140426123508BST.87y4ys5no3.fsf@bsb.me.uk> |
| In reply to | #23947 |
jonas.thornvall@gmail.com writes:
<snip>
> To me it seem like a problem above the actual scripting technic more
> like a problem when constructing a program language. But maybe i am
> missing something fairly obvious here?
It sounds like you need a function. If I understood, you have something
you want to do with the elements of two arrays A and B, but only with
those of B if some condition is true. You don't want the test to run
lots of times:
for (var i = 0; i < lots_and_lots; i++) {
do_something(A[i]);
if (C) do_something(B[i]);
}
and you don't want to duplicate the loop:
for (var i = 0; i < lots_and_lots; i++)
do_something(A[i]);
if (C)
for (var i = 0; i < lots_and_lots; i++)
do_something(A[i]);
The usual solution is a function:
do_something_for_all(A);
if (C) do_something_for_all(B);
Recent editions of the ECMAScript specification have functions that can
apply a function for the elements of an array. See, for example, 'map',
'every' and 'forEach'.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-26 07:59 -0700 |
| Message-ID | <f4a2b34c-d7b7-4974-a6f6-1582ce0045ff@googlegroups.com> |
| In reply to | #23954 |
Den lördagen den 26:e april 2014 kl. 13:35:08 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> <snip>
>
> > To me it seem like a problem above the actual scripting technic more
>
> > like a problem when constructing a program language. But maybe i am
>
> > missing something fairly obvious here?
>
>
>
> It sounds like you need a function. If I understood, you have something
>
> you want to do with the elements of two arrays A and B, but only with
>
> those of B if some condition is true. You don't want the test to run
>
> lots of times:
>
>
>
> for (var i = 0; i < lots_and_lots; i++) {
>
> do_something(A[i]);
>
> if (C) do_something(B[i]);
>
> }
>
>
>
> and you don't want to duplicate the loop:
>
>
>
> for (var i = 0; i < lots_and_lots; i++)
>
> do_something(A[i]);
>
> if (C)
>
> for (var i = 0; i < lots_and_lots; i++)
>
> do_something(A[i]);
>
>
>
> The usual solution is a function:
>
>
>
> do_something_for_all(A);
>
> if (C) do_something_for_all(B);
>
>
>
> Recent editions of the ECMAScript specification have functions that can
>
> apply a function for the elements of an array. See, for example, 'map',
>
> 'every' and 'forEach'.
>
>
>
> --
>
> Ben.
But that will still run a loop in each leg although technically only one loop required? Problem is if you have like fifty variables and fifty conditions it will craate alot of overhead?
Is there any programming language where a flag is set for the condition to do something on a variable, so it do not have to be checked again and again as long running in the loop?
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-26 08:11 -0700 |
| Message-ID | <088ba8a1-abe5-4132-a7c6-cdf35d5c3066@googlegroups.com> |
| In reply to | #23956 |
Den lördagen den 26:e april 2014 kl. 16:59:06 UTC+2 skrev jonas.t...@gmail.com:
> Den lördagen den 26:e april 2014 kl. 13:35:08 UTC+2 skrev Ben Bacarisse:
>
> > jonas.thornvall@gmail.com writes:
>
> >
>
> > <snip>
>
> >
>
> > > To me it seem like a problem above the actual scripting technic more
>
> >
>
> > > like a problem when constructing a program language. But maybe i am
>
> >
>
> > > missing something fairly obvious here?
>
> >
>
> >
>
> >
>
> > It sounds like you need a function. If I understood, you have something
>
> >
>
> > you want to do with the elements of two arrays A and B, but only with
>
> >
>
> > those of B if some condition is true. You don't want the test to run
>
> >
>
> > lots of times:
>
> >
>
> >
>
> >
>
> > for (var i = 0; i < lots_and_lots; i++) {
>
> >
>
> > do_something(A[i]);
>
> >
>
> > if (C) do_something(B[i]);
>
> >
>
> > }
>
> >
>
> >
>
> >
>
> > and you don't want to duplicate the loop:
>
> >
>
> >
>
> >
>
> > for (var i = 0; i < lots_and_lots; i++)
>
> >
>
> > do_something(A[i]);
>
> >
>
> > if (C)
>
> >
>
> > for (var i = 0; i < lots_and_lots; i++)
>
> >
>
> > do_something(A[i]);
>
> >
>
> >
>
> >
>
> > The usual solution is a function:
>
> >
>
> >
>
> >
>
> > do_something_for_all(A);
>
> >
>
> > if (C) do_something_for_all(B);
>
> >
>
> >
>
> >
>
> > Recent editions of the ECMAScript specification have functions that can
>
> >
>
> > apply a function for the elements of an array. See, for example, 'map',
>
> >
>
> > 'every' and 'forEach'.
>
> >
>
> >
>
> >
>
> > --
>
> >
>
> > Ben.
>
> But that will still run a loop in each leg although technically only one loop required? Problem is if you have like fifty variables and fifty conditions it will craate alot of overhead?
>
>
>
> Is there any programming language where a flag is set for the condition to do something on a variable, so it do not have to be checked again and again as long running in the loop?
Would it not be neat with a programming language where the default flag for an IF clause is set to 0.
But as soon you set that if clause to 1 the statement expression following always executed.
That way you only need an initial check, and if your requirments fullfilled the if clause is skipped?
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2014-04-26 22:34 +0100 |
| Message-ID | <0.6fc64329016b6998cc1a.20140426223412BST.87a9b76ai3.fsf@bsb.me.uk> |
| In reply to | #23956 |
jonas.thornvall@gmail.com writes: <snip> > But that will still run a loop in each leg although technically only > one loop required? I tried to be clear, but if it isn't you are probably best off ignoring what I wrote. > Problem is if you have like fifty variables and > fifty conditions it will craate alot of overhead? I don't see why, but maybe I don't know what "overhead" means to you. To me, a complex system with dozens of conditions controlling actions on dozens of variables would be programmed quite differently, but the emphasis would be on clarity. It's going to be unreadable otherwise. <snip> -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-27 00:44 -0700 |
| Message-ID | <2f7c3181-f823-4cbf-9f86-c3d43e96f5cf@googlegroups.com> |
| In reply to | #23963 |
Den lördagen den 26:e april 2014 kl. 23:34:12 UTC+2 skrev Ben Bacarisse: > jonas.thornvall@gmail.com writes: > > <snip> > > > But that will still run a loop in each leg although technically only > > > one loop required? > > > > I tried to be clear, but if it isn't you are probably best off ignoring > > what I wrote. > > > > > Problem is if you have like fifty variables and > > > fifty conditions it will craate alot of overhead? > > > > I don't see why, but maybe I don't know what "overhead" means to you. > > To me, a complex system with dozens of conditions controlling actions on > > dozens of variables would be programmed quite differently, but the > > emphasis would be on clarity. It's going to be unreadable otherwise. > > > > <snip> > > -- > > Ben. I think you are totally wrong Ben, we are basicly speaking about a print/write function, and if one should be able to steer what will be printout *before* one goes into the loop. Otherwise you will have to *fiddle* and doing unneccessary checks *within* loop. If you can not see this, you may still be a good programmer but i am sure any program language you developed would be *convoluted*. This is not even logic, its mostly common sense and i find it a bit weird that some people whose profession is programming totally lose it. Ben when you print out something in a loop, and there is 50 conditions *on printouts* that is clear set before starting print/write, you do not want to create a separate function for each or create a loop leg for each? It could be so simple that the IF clause are named (could be named and unnamed) ifA ifB ifAA and so on and have a boolean flag. The default could be set to false the condition is not fullfilled, but you are able to set the boolean flag to true. The if statement already fullfilled no need to check again. Rather then looping and checking 50 *unnecessary* conditions for each round of loop printing out millions of values. No and i do not prefer 50 loops running in 50 functions. I prefer a simple boolean switch upon the if clause, and it is a mystery that i do not know any programming that can handle such a simple task.
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2014-04-27 13:20 +0100 |
| Message-ID | <270420141320426439%timstreater@greenbee.net> |
| In reply to | #23968 |
In article <2f7c3181-f823-4cbf-9f86-c3d43e96f5cf@googlegroups.com>,
<jonas.thornvall@gmail.com> wrote:
> I think you are totally wrong Ben, we are basicly speaking about a
> print/write function, and if one should be able to steer what will be
> printout *before* one goes into the loop.
>
> Otherwise you will have to *fiddle* and doing unneccessary checks *within*
> loop.
>
> If you can not see this, you may still be a good programmer but i am sure any
> program language you developed would be *convoluted*.
>
> This is not even logic, its mostly common sense and i find it a bit weird
> that some people whose profession is programming totally lose it.
I haven't been following this from the beginning, so I am not quite
sure what is being asked for here. ISTM that you are talking about
printing inside a loop, with perhaps a condition attached to the
printing. Something like:
for (i=0; i<limit; i++)
{
if (cond) print ( ... );
}
Now you are saying that cond is perhaps complicated and you don't want
to be recomputing that each time around the loop.
I see two possibilities:
1) cond does not depend on the loop counter, i.
2) cond *does* depend on the loop counter, i.
In case (1) you simply optimise it by taking the if out of the loop and
doing:
if (cond)
{
for (i=0; i<limit; i++)
{
print ( ... );
}
}
In case (2) you *cannot* do that and you *must* leave the if as it is.
This is true whether or not cond is simple or complicated.
If you do not understand why this is so, then I suggest you need to
learn how computer processors work.
Or are you asking a different question altogether? If so then you have
not expressed it clearly.
--
"The idea that Bill Gates has appeared like a knight in shining armour to
lead all customers out of a mire of technological chaos neatly ignores
the fact that it was he who, by peddling second-rate technology, led them
into it in the first place." - Douglas Adams
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-27 06:09 -0700 |
| Message-ID | <d38432b8-8e55-4cb1-8fa3-5281d18c89e6@googlegroups.com> |
| In reply to | #23972 |
Den söndagen den 27:e april 2014 kl. 14:20:42 UTC+2 skrev Tim Streater:
> In article <2f7c3181-f823-4cbf-9f86-c3d43e96f5cf@googlegroups.com>,
>
> <jonas.thornvall@gmail.com> wrote:
>
>
>
> > I think you are totally wrong Ben, we are basicly speaking about a
>
> > print/write function, and if one should be able to steer what will be
>
> > printout *before* one goes into the loop.
>
> >
>
> > Otherwise you will have to *fiddle* and doing unneccessary checks *within*
>
> > loop.
>
> >
>
> > If you can not see this, you may still be a good programmer but i am sure any
>
> > program language you developed would be *convoluted*.
>
> >
>
> > This is not even logic, its mostly common sense and i find it a bit weird
>
> > that some people whose profession is programming totally lose it.
>
>
>
> I haven't been following this from the beginning, so I am not quite
>
> sure what is being asked for here. ISTM that you are talking about
>
> printing inside a loop, with perhaps a condition attached to the
>
> printing. Something like:
>
>
>
> for (i=0; i<limit; i++)
>
> {
>
> if (cond) print ( ... );
>
> }
>
>
>
> Now you are saying that cond is perhaps complicated and you don't want
>
> to be recomputing that each time around the loop.
>
>
>
> I see two possibilities:
>
>
>
> 1) cond does not depend on the loop counter, i.
>
>
>
> 2) cond *does* depend on the loop counter, i.
>
>
>
>
>
> In case (1) you simply optimise it by taking the if out of the loop and
>
> doing:
>
>
>
> if (cond)
>
> {
>
> for (i=0; i<limit; i++)
>
> {
>
> print ( ... );
>
> }
>
> }
>
>
>
> In case (2) you *cannot* do that and you *must* leave the if as it is.
>
> This is true whether or not cond is simple or complicated.
>
>
>
> If you do not understand why this is so, then I suggest you need to
>
> learn how computer processors work.
>
>
>
> Or are you asking a different question altogether? If so then you have
>
> not expressed it clearly.
>
>
>
> --
>
> "The idea that Bill Gates has appeared like a knight in shining armour to
>
> lead all customers out of a mire of technological chaos neatly ignores
>
> the fact that it was he who, by peddling second-rate technology, led them
>
> into it in the first place." - Douglas Adams
In this case it is not dependent upon the loop counter, but it really isn't the issue.
The issue is.
You are printing one million of pages, paper or screen doesn't matter.
Depending on the date some stuff will be printed, each holiday and weekend there is things that will be printed. But on ordinary weekdays these will not.
The thing is the newspaper it printed everyday in 200 000 copies, you do not want to check for each newspaper in loop what date it is would you?
You know what date it is don't you?
In my case i have an array multidimensional array (object?) with properties that are set before i print in loop but some properties will only be printed, if they are bigger then a certain size. Because it does not make sense print things that aren't readable... But you you can see the colors specks...
Yes i know the size and if the property should be printed, there is no difference from the analogy above. You simply do not recheck things where you already know the condition met or not.
But the good programmers of javascript (well pretty much any language) suggest, that I make a function for each conditional print statement.
And i simply try to tell that is not a good way todo things.
And it is not a good idea to make 50 loop legs, even if they run in separate functions. Even for all and for each is not, that handy solution.
The solution is that a condition that is set true before entering loop is superflous, unless the variables of the condition change it should *always* be done.
But this would require a boolean flag on the if statement wheter the code should be run or omitted. Thus the if condition itself be a variable.
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2014-04-27 18:30 +0100 |
| Message-ID | <270420141830040121%timstreater@greenbee.net> |
| In reply to | #23974 |
In article <d38432b8-8e55-4cb1-8fa3-5281d18c89e6@googlegroups.com>, <jonas.thornvall@gmail.com> wrote: > In this case it is not dependent upon the loop counter, but it really isn't > the issue. > > The issue is. > > You are printing one million of pages, paper or screen doesn't matter. > Depending on the date some stuff will be printed, each holiday and weekend > there is things that will be printed. But on ordinary weekdays these will > not. > > The thing is the newspaper it printed everyday in 200 000 copies, you do not > want to check for each newspaper in loop what date it is would you? In which case I'd check the day of the week first and have a separate section of code for the *major* conditions. Sounds like you might have several different such sections, maybe 7 or 8. > In my case i have an array multidimensional array (object?) with properties > that are set before i print in loop but some properties will only be printed, > if they are bigger then a certain size. Because it does not make sense print > things that aren't readable... But you you can see the colors specks... Then you build your image to be printed (different depending on the date) just once, and you print the same image 200,000 times. You might have a different function (that you call once) to build the image. It's not actually necessary to have these functions but it makes the code more modular and easier to understand. I could imagine that each of these functions might be quite big, but there will be separate sections of code that can also be put into functions that will reduce the overall size. If you have a clear idea in your head of the process then you can design the structure more easily and break the problem down into smaller parts. -- "People don't buy Microsoft for quality, they buy it for compatibility with what Bob in accounting bought last year. Trace it back - they buy Microsoft because the IBM Selectric didn't suck much" - P Seebach, afc
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-27 12:13 -0700 |
| Message-ID | <3b416ba7-6afc-46e5-8f86-b6f1483f8ad3@googlegroups.com> |
| In reply to | #23980 |
Den söndagen den 27:e april 2014 kl. 19:30:04 UTC+2 skrev Tim Streater: > In article <d38432b8-8e55-4cb1-8fa3-5281d18c89e6@googlegroups.com>, > > <jonas.thornvall@gmail.com> wrote: > > > > > In this case it is not dependent upon the loop counter, but it really isn't > > > the issue. > > > > > > The issue is. > > > > > > You are printing one million of pages, paper or screen doesn't matter. > > > Depending on the date some stuff will be printed, each holiday and weekend > > > there is things that will be printed. But on ordinary weekdays these will > > > not. > > > > > > The thing is the newspaper it printed everyday in 200 000 copies, you do not > > > want to check for each newspaper in loop what date it is would you? > > > > In which case I'd check the day of the week first and have a separate > > section of code for the *major* conditions. Sounds like you might have > > several different such sections, maybe 7 or 8. > > > > > In my case i have an array multidimensional array (object?) with properties > > > that are set before i print in loop but some properties will only be printed, > > > if they are bigger then a certain size. Because it does not make sense print > > > things that aren't readable... But you you can see the colors specks... > > > > Then you build your image to be printed (different depending on the > > date) just once, and you print the same image 200,000 times. You might > > have a different function (that you call once) to build the image. It's > > not actually necessary to have these functions but it makes the code > > more modular and easier to understand. I could imagine that each of > > these functions might be quite big, but there will be separate sections > > of code that can also be put into functions that will reduce the > > overall size. > > > > If you have a clear idea in your head of the process then you can > > design the structure more easily and break the problem down into > > smaller parts. > > > > -- > > "People don't buy Microsoft for quality, they buy it for compatibility > > with what Bob in accounting bought last year. Trace it back - they buy > > Microsoft because the IBM Selectric didn't suck much" - P Seebach, afc It is a user selection newspaper... Joke aside it is not really a newspaper look at the example it is a matter of being able todo an animation on the fly without having to check the size of font while Printing out objects doing it. It would of course look like a wise choice to create a separate function for fillText in the end. But that beg the question, which effort take more Resources and time. And i Think i already know tha answer, since each element with text i write out is on different cordinates, i can not print them out without the cordinates given by the loop. And in the end 2 000 000 checks regarding the font size is less work then 2 000 000 function calls... And i do realise that the way i did break out the paint_out from loop with function calls did create unnecessary overhead, but i was tiding up the code a Little for better readability but lost speed, but it probably go back in the end. And it seem with current programming paradigm it not that much todo regarding the 2 000 000 unecesssary checks of font size. Because there is no way breaking out the fillText from fillRect because of the pairwise printouts on different cordinates, thus making it impossible running fillText without recreating a full identical loop or alternatively store the cordinates.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-28 02:33 -0700 |
| Message-ID | <1a434311-60b2-44ac-b930-31c2f817f777@googlegroups.com> |
| In reply to | #23981 |
Den söndagen den 27:e april 2014 kl. 21:13:30 UTC+2 skrev jonas.t...@gmail.com: > Den söndagen den 27:e april 2014 kl. 19:30:04 UTC+2 skrev Tim Streater: > > > In article <d38432b8-8e55-4cb1-8fa3-5281d18c89e6@googlegroups.com>, > > > > > > <jonas.thornvall@gmail.com> wrote: > > > > > > > > > > > > > In this case it is not dependent upon the loop counter, but it really isn't > > > > > > > the issue. > > > > > > > > > > > > > > The issue is. > > > > > > > > > > > > > > You are printing one million of pages, paper or screen doesn't matter. > > > > > > > Depending on the date some stuff will be printed, each holiday and weekend > > > > > > > there is things that will be printed. But on ordinary weekdays these will > > > > > > > not. > > > > > > > > > > > > > > The thing is the newspaper it printed everyday in 200 000 copies, you do not > > > > > > > want to check for each newspaper in loop what date it is would you? > > > > > > > > > > > > In which case I'd check the day of the week first and have a separate > > > > > > section of code for the *major* conditions. Sounds like you might have > > > > > > several different such sections, maybe 7 or 8. > > > > > > > > > > > > > In my case i have an array multidimensional array (object?) with properties > > > > > > > that are set before i print in loop but some properties will only be printed, > > > > > > > if they are bigger then a certain size. Because it does not make sense print > > > > > > > things that aren't readable... But you you can see the colors specks... > > > > > > > > > > > > Then you build your image to be printed (different depending on the > > > > > > date) just once, and you print the same image 200,000 times. You might > > > > > > have a different function (that you call once) to build the image. It's > > > > > > not actually necessary to have these functions but it makes the code > > > > > > more modular and easier to understand. I could imagine that each of > > > > > > these functions might be quite big, but there will be separate sections > > > > > > of code that can also be put into functions that will reduce the > > > > > > overall size. > > > > > > > > > > > > If you have a clear idea in your head of the process then you can > > > > > > design the structure more easily and break the problem down into > > > > > > smaller parts. > > > > > > > > > > > > -- > > > > > > "People don't buy Microsoft for quality, they buy it for compatibility > > > > > > with what Bob in accounting bought last year. Trace it back - they buy > > > > > > Microsoft because the IBM Selectric didn't suck much" - P Seebach, afc > > > > It is a user selection newspaper... Joke aside it is not really a newspaper look at the example it is a matter of being able todo an animation on the fly without having to check the size of font while Printing out objects doing it. > > > > It would of course look like a wise choice to create a separate function for fillText in the end. > > > > But that beg the question, which effort take more Resources and time. And i Think i already know tha answer, since each element with text i write out is on different cordinates, i can not print them out without the cordinates given by the loop. > > > > And in the end 2 000 000 checks regarding the font size is less work then 2 000 000 function calls... > > > > And i do realise that the way i did break out the paint_out from loop with function calls did create unnecessary overhead, but i was tiding up the code a Little for better readability but lost speed, but it probably go back in the end. > > > > And it seem with current programming paradigm it not that much todo regarding the 2 000 000 unecesssary checks of font size. > > > > Because there is no way breaking out the fillText from fillRect because of the pairwise printouts on different cordinates, thus making it impossible running fillText without recreating a full identical loop or alternatively store the cordinates. To store the cordinates and run a second loop if requsites met is the fastest approach/solution i can think off, and really no problem on modern computer. But it is kind of ugly isn't it?
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-27 06:25 -0700 |
| Message-ID | <ee94511b-b856-42ff-944d-d20cbea814b8@googlegroups.com> |
| In reply to | #23972 |
Den söndagen den 27:e april 2014 kl. 14:20:42 UTC+2 skrev Tim Streater:
> In article <2f7c3181-f823-4cbf-9f86-c3d43e96f5cf@googlegroups.com>,
>
> <jonas.thornvall@gmail.com> wrote:
>
>
>
> > I think you are totally wrong Ben, we are basicly speaking about a
>
> > print/write function, and if one should be able to steer what will be
>
> > printout *before* one goes into the loop.
>
> >
>
> > Otherwise you will have to *fiddle* and doing unneccessary checks *within*
>
> > loop.
>
> >
>
> > If you can not see this, you may still be a good programmer but i am sure any
>
> > program language you developed would be *convoluted*.
>
> >
>
> > This is not even logic, its mostly common sense and i find it a bit weird
>
> > that some people whose profession is programming totally lose it.
>
>
>
> I haven't been following this from the beginning, so I am not quite
>
> sure what is being asked for here. ISTM that you are talking about
>
> printing inside a loop, with perhaps a condition attached to the
>
> printing. Something like:
>
>
>
> for (i=0; i<limit; i++)
>
> {
>
> if (cond) print ( ... );
>
> }
>
>
>
> Now you are saying that cond is perhaps complicated and you don't want
>
> to be recomputing that each time around the loop.
>
>
>
> I see two possibilities:
>
>
>
> 1) cond does not depend on the loop counter, i.
>
>
>
> 2) cond *does* depend on the loop counter, i.
>
>
>
>
>
> In case (1) you simply optimise it by taking the if out of the loop and
>
> doing:
>
>
>
> if (cond)
>
> {
>
> for (i=0; i<limit; i++)
>
> {
>
> print ( ... );
>
> }
>
> }
>
>
>
> In case (2) you *cannot* do that and you *must* leave the if as it is.
>
> This is true whether or not cond is simple or complicated.
>
>
>
> If you do not understand why this is so, then I suggest you need to
>
> learn how computer processors work.
>
>
>
> Or are you asking a different question altogether? If so then you have
>
> not expressed it clearly.
>
>
>
> --
>
> "The idea that Bill Gates has appeared like a knight in shining armour to
>
> lead all customers out of a mire of technological chaos neatly ignores
>
> the fact that it was he who, by peddling second-rate technology, led them
>
> into it in the first place." - Douglas Adams
Here you can see the actual example look at function paint_out()
http://web.comhem.se/jonasth/prime0.993/div.html
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-04-27 14:55 +0000 |
| Message-ID | <ljj5pr$qlv$1@dont-email.me> |
| In reply to | #23972 |
On Sun, 27 Apr 2014 13:20:42 +0100, Tim Streater wrote:
> In article <2f7c3181-f823-4cbf-9f86-c3d43e96f5cf@googlegroups.com>,
> <jonas.thornvall@gmail.com> wrote:
> I see two possibilities:
>
> 1) cond does not depend on the loop counter, i.
>
> 2) cond *does* depend on the loop counter, i.
>
> In case (1) you simply optimise it by taking the if out of the loop
> In case (2) you *cannot* do that and you *must* leave the if [in the
loop].
> This is true whether or not cond is simple or complicated.
There may be a third possibility:
Split the condition.
if ( non loop dependant part of condition )
for ( each thing in loop )
if ( loop dependant part of condition )
{ /* do whatever needs to be done */ }
Or pre-calculate as much as the condition as possible outside the loop,
so that the part of the condition being calculated inside the loop is
less.
But, as I think someone else already said, beware of premature
optimisation. Although I suspect that the dynamic optimisation of loops
in script processors is probably a lot less than that provided in modern
compilers.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2014-04-27 15:41 +0100 |
| Message-ID | <0.9138ad1e35f363b22309.20140427154141BST.87r44i3kd6.fsf@bsb.me.uk> |
| In reply to | #23968 |
jonas.thornvall@gmail.com writes: > Den lördagen den 26:e april 2014 kl. 23:34:12 UTC+2 skrev Ben Bacarisse: >> jonas.thornvall@gmail.com writes: >> <snip> >> > But that will still run a loop in each leg although technically only >> > one loop required? >> >> I tried to be clear, but if it isn't you are probably best off ignoring >> what I wrote. >> >> > Problem is if you have like fifty variables and >> > fifty conditions it will craate alot of overhead? >> >> I don't see why, but maybe I don't know what "overhead" means to you. >> To me, a complex system with dozens of conditions controlling actions on >> dozens of variables would be programmed quite differently, but the >> emphasis would be on clarity. It's going to be unreadable otherwise. <snip> > I think you are totally wrong Ben, we are basicly speaking about a > print/write function, and if one should be able to steer what will be > printout *before* one goes into the loop. > > Otherwise you will have to *fiddle* and doing unneccessary checks > *within* loop. That's why I suggested moving the test outside, but it does not fit for every situation. Your example was not clear on what the constraints were so I could do no more that guess at a possible alternative. > If you can not see this, you may still be a good programmer but i am > sure any program language you developed would be *convoluted*. That's kind of you, not! > Ben when you print out something in a loop, and there is 50 conditions > *on printouts* that is clear set before starting print/write, you do > not want to create a separate function for each or create a loop leg > for each? No I don't (and I hope it did not seem like I was suggesting that). The trouble with vague descriptions like "50 conditions" is that it matters what they are and what they control. My suggestion was based on a guess, which in turn was based on your example with a single condition. > It could be so simple that the IF clause are named (could be named and > unnamed) ifA ifB ifAA and so on and have a boolean flag. The default > could be set to false the condition is not fullfilled, but you are > able to set the boolean flag to true. The if statement already > fullfilled no need to check again. There are ways to do that in conventional programming languages but they will all be unacceptable to you. > Rather then looping and checking 50 *unnecessary* conditions for each > round of loop printing out millions of values. No and i do not prefer > 50 loops running in 50 functions. Which no one has suggested. 50 function call might, however, be a reasonable solution (remember, the designers of ECMAScript have omitted the feature you want, so you must make do with what they did think of providing). > I prefer a simple boolean switch upon the if clause, and it is a > mystery that i do not know any programming that can handle such a > simple task. I used to think that all the time. Every time I came across a programming pattern that was not supported directly by the language I was using that day, I thought, "what Fortran, SASL, LISP, BCPL, Algol 68, Snobol (or whatever) needs is feature X or Y or Z". Sometimes that's the right thought, and sometimes it isn't. It's surprisingly hard to know when it is and when it isn't. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-27 13:57 -0700 |
| Message-ID | <94ff2120-b86f-4d73-b5ca-3e38cd1dde7d@googlegroups.com> |
| In reply to | #23978 |
Den söndagen den 27:e april 2014 kl. 16:41:41 UTC+2 skrev Ben Bacarisse: > jonas.thornvall@gmail.com writes: > > > > > Den lördagen den 26:e april 2014 kl. 23:34:12 UTC+2 skrev Ben Bacarisse: > > >> jonas.thornvall@gmail.com writes: > > >> <snip> > > >> > But that will still run a loop in each leg although technically only > > >> > one loop required? > > >> > > >> I tried to be clear, but if it isn't you are probably best off ignoring > > >> what I wrote. > > >> > > >> > Problem is if you have like fifty variables and > > >> > fifty conditions it will craate alot of overhead? > > >> > > >> I don't see why, but maybe I don't know what "overhead" means to you. > > >> To me, a complex system with dozens of conditions controlling actions on > > >> dozens of variables would be programmed quite differently, but the > > >> emphasis would be on clarity. It's going to be unreadable otherwise. > > <snip> > > > > > I think you are totally wrong Ben, we are basicly speaking about a > > > print/write function, and if one should be able to steer what will be > > > printout *before* one goes into the loop. > > > > > > Otherwise you will have to *fiddle* and doing unneccessary checks > > > *within* loop. > > > > That's why I suggested moving the test outside, but it does not fit for > > every situation. Your example was not clear on what the constraints > > were so I could do no more that guess at a possible alternative. > > > > > If you can not see this, you may still be a good programmer but i am > > > sure any program language you developed would be *convoluted*. > > > > That's kind of you, not! > > > > > Ben when you print out something in a loop, and there is 50 conditions > > > *on printouts* that is clear set before starting print/write, you do > > > not want to create a separate function for each or create a loop leg > > > for each? > > > > No I don't (and I hope it did not seem like I was suggesting that). The > > trouble with vague descriptions like "50 conditions" is that it matters > > what they are and what they control. My suggestion was based on a > > guess, which in turn was based on your example with a single condition. > > > > > It could be so simple that the IF clause are named (could be named and > > > unnamed) ifA ifB ifAA and so on and have a boolean flag. The default > > > could be set to false the condition is not fullfilled, but you are > > > able to set the boolean flag to true. The if statement already > > > fullfilled no need to check again. > > > > There are ways to do that in conventional programming languages but they > > will all be unacceptable to you. > > > > > Rather then looping and checking 50 *unnecessary* conditions for each > > > round of loop printing out millions of values. No and i do not prefer > > > 50 loops running in 50 functions. > > > > Which no one has suggested. 50 function call might, however, be a > > reasonable solution (remember, the designers of ECMAScript have omitted > > the feature you want, so you must make do with what they did think of > > providing). > > > > > I prefer a simple boolean switch upon the if clause, and it is a > > > mystery that i do not know any programming that can handle such a > > > simple task. > > > > I used to think that all the time. Every time I came across a > > programming pattern that was not supported directly by the language I > > was using that day, I thought, "what Fortran, SASL, LISP, BCPL, Algol > > 68, Snobol (or whatever) needs is feature X or Y or Z". Sometimes > > that's the right thought, and sometimes it isn't. It's surprisingly > > hard to know when it is and when it isn't. > > > > -- > > Ben. I could surprise you with my cobol skills ;D
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-25 23:18 -0700 |
| Message-ID | <bfbecbb2-47de-4cc4-b40e-348992c5e71b@googlegroups.com> |
| In reply to | #23946 |
Den lördagen den 26:e april 2014 kl. 07:53:59 UTC+2 skrev jonas.t...@gmail.com: > I find some things causing logical problems in programming. > > > > I have a loop, that will print out two arrays filled with element A and B. > > > > Now B have prerequisite that it will only be printout when x<3 dealing with Writing to canvas i want todo so Little operations as possible in the actual printout. And x is set Before we go into loop? > > > > Of course i do not want to check for each element, because x is already set when i go into loop. > > > > > > But it also seem weird that i should need to run two legs, e.g two loops one for A and one for B due to a prerequisite on B? > > > > I mean let us say there was 50 variables and different prerequisites instead of two? I can hardly run each printout in its own leg? > > > > This problem must have a general solution that i have learned but forgotten? > > > > How is this generally solved? Maybe this is impossible without making the A and B object properties of the loop, making the loops named objects with properties?
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-25 23:23 -0700 |
| Message-ID | <8366abc9-e163-4f16-a267-85dd36651921@googlegroups.com> |
| In reply to | #23946 |
Den lördagen den 26:e april 2014 kl. 07:53:59 UTC+2 skrev jonas.t...@gmail.com: > I find some things causing logical problems in programming. > > > > I have a loop, that will print out two arrays filled with element A and B. > > > > Now B have prerequisite that it will only be printout when x<3 dealing with Writing to canvas i want todo so Little operations as possible in the actual printout. And x is set Before we go into loop? > > > > Of course i do not want to check for each element, because x is already set when i go into loop. > > > > > > But it also seem weird that i should need to run two legs, e.g two loops one for A and one for B due to a prerequisite on B? > > > > I mean let us say there was 50 variables and different prerequisites instead of two? I can hardly run each printout in its own leg? > > > > This problem must have a general solution that i have learned but forgotten? > > > > How is this generally solved? I forgot to tell you A and B are properties of objects/elements stored in an Array. B is a text that only will be printout to canvas when x<4 and the other just the color of an object printed out, but there could be millions stored. And it seem a Little bit overhead to check a million times in the loop.
[toc] | [prev] | [next] | [standalone]
| From | Christoph Michael Becker <cmbecker69@arcor.de> |
|---|---|
| Date | 2014-04-26 15:06 +0200 |
| Message-ID | <535baf47$0$6700$9b4e6d93@newsspool2.arcor-online.net> |
| In reply to | #23946 |
jonas.thornvall@gmail.com wrote: > I find some things causing logical problems in programming. > > I have a loop, that will print out two arrays filled with element A > and B. > > Now B have prerequisite that it will only be printout when x<3 > dealing with Writing to canvas i want todo so Little operations as > possible in the actual printout. And x is set Before we go into > loop? > > Of course i do not want to check for each element, because x is > already set when i go into loop. Do the comparision before the loop, and store the result in a variable as a boolean flag. Then just check this flag inside the loop. That might still seem like overhead, but actually, especially when you're doing canvas operations inside the loop, you can most likely neglect it. And generally, you should beware of premature optimization. -- Christoph M. Becker
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2014-04-26 08:00 -0700 |
| Message-ID | <4d4a71cf-f931-4158-a0f8-b7fdcdbbc0b7@googlegroups.com> |
| In reply to | #23955 |
Den lördagen den 26:e april 2014 kl. 15:06:24 UTC+2 skrev Christoph Michael Becker: > jonas.thornvall@gmail.com wrote: > > > > > I find some things causing logical problems in programming. > > > > > > I have a loop, that will print out two arrays filled with element A > > > and B. > > > > > > Now B have prerequisite that it will only be printout when x<3 > > > dealing with Writing to canvas i want todo so Little operations as > > > possible in the actual printout. And x is set Before we go into > > > loop? > > > > > > Of course i do not want to check for each element, because x is > > > already set when i go into loop. > > > > Do the comparision before the loop, and store the result in a variable > > as a boolean flag. Then just check this flag inside the loop. > > > > That might still seem like overhead, but actually, especially when > > you're doing canvas operations inside the loop, you can most likely > > neglect it. > > > > And generally, you should beware of premature optimization. > > > > -- > > Christoph M. Becker Pretty close to what i was thinking.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.javascript
csiph-web