php-general Digest 2 Aug 2009 18:24:59 -0000 Issue 6263

Topics (messages 296174 through 296182):

Re: PHP programming strategy
        296174 by: Eddie Drapkin
        296176 by: Paul M Foster
        296182 by: Larry Garfield

Re: Problem: Writing into Files?
        296175 by: Parham Doustdar
        296177 by: Richard Heyes
        296178 by: Ollisso
        296179 by: Parham Doustdar
        296180 by: Parham Doustdar

Notification system
        296181 by: Dušan Novaković

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
> I actually benchmarked that once.  I had a reasonably large PHP file that was,
> in fact, over 50% docblocks.  That's not even counting inline comments.  While
> trying to find things to optimize, removing about 800 lines worth of comments
> (all of the docblocks) did, in fact, produce a noticeable performance
> difference.  It was only barely noticeable, but it just barely registered as
> more than random sampling jitter.  I actually concluded that if cutting the
> file *in half* was only just barely noticeable, then it really wasn't worth 
> the
> effort.

Yeah but what happens if you run the script through the tokenizer and
strip ALL comments, unnecessary whitespace, newline characters, etc.
out?

> Larry Garfield
> la...@garfieldtech.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--- End Message ---
--- Begin Message ---
On Sun, Aug 02, 2009 at 11:25:40AM +1000, Clancy wrote:

> Is anyone here interested in discussing programming strategy, or or know
> of a discussion
> group which is interested in the subject?
> 
> The sorts of questions I am interested in are:
> 
> 1. I have a highly variable program which always shows the same page,
> but which includes
> different modules to give different results.  The various modules call
> on different
> functions. Is it better to minimise memory by including only the functions
> actually
> required, but increase the number of files included, or to bundle all the
> functions into
> one file, thereby reducing the number of files included but increasing
> the size in memory?

My advice would be to only load the modules you need, no matter how
convoluted the logic you must use to do that. A lot of PHP programmers
are very sloppy about memory usage, falling back on the cache argument,
etc. But there's no reason to use more memory than you have to.

However, there is an issue if all these modules are scattered about
everywhere. You also have to look at the overhead on the server when
it's having to open 15 files for every page displayed. That's silly as
well.

So it's a trade-off. If you have the latter problem, I'd probably opt to
put everything in one or a few files. In my opinion, disk thrashing on a
busy internet server is worse than gobbling up more memory. That disk
thrashing is likely to have a negative effect on all the other users of
that server.

> 
> 2. As PHP is an interpreted program comments certainly increase the memory
> requirements,
> and presumably they slow down the operation of the program. Would stripping
> out the
> comments before uploading the production version give any visible
> improvement in
> performance?
> 

I bow to those who have profiled this, as far as performance goes.
However, remember, you have to *maintain* this code. In a trade-off
between performance and maintainability, I'd opt for maintainability.
You'll thank yourself in the future.

Paul


-- 
Paul M. Foster

--- End Message ---
--- Begin Message ---
On Saturday 01 August 2009 11:01:11 pm Eddie Drapkin wrote:
> > I actually benchmarked that once.  I had a reasonably large PHP file that
> > was, in fact, over 50% docblocks.  That's not even counting inline
> > comments.  While trying to find things to optimize, removing about 800
> > lines worth of comments (all of the docblocks) did, in fact, produce a
> > noticeable performance difference.  It was only barely noticeable, but it
> > just barely registered as more than random sampling jitter.  I actually
> > concluded that if cutting the file *in half* was only just barely
> > noticeable, then it really wasn't worth the effort.
>
> Yeah but what happens if you run the script through the tokenizer and
> strip ALL comments, unnecessary whitespace, newline characters, etc.
> out?

Honestly?  I think you'll save more CPU time by eliminating one SQL query.  
Most files are not 60% comments.  In a file that is only about 20% comments, I 
doubt you could even measure the difference.  There are far far far more useful 
ways to optimize your code.

(Note that this is different for CSS or Javascript, where compressors like that 
are commonplace because you have to transfer the entire file over the network 
repeatedly, which is a few orders of magnitude slower than system memory.  
Compressors and aggregators there make sense.  PHP code never leaves the 
server, so those benefits don't exist.)

-- 
Larry Garfield
la...@garfieldtech.com

--- End Message ---
--- Begin Message ---
Dear Ollisso,
I tried it with FLock() but it still didn't work. This is what I did:
[code]
$fp = fopen($f, "r");
$count =fgets($fp, 1024);
fclose($fp);
$fw = fopen($f, "w");
while (!flock($fw, LOCK_EX))
sleep(1);
$cnew = $count + 1;
$countnew = fputs($fw, $count + 1);
flock($fw, LOCK_UN);
fclose($fw);
[/code]

Am I doing anything wrong here?
Thanks!
-- 
---
Contact info:
Skype: parham-d
MSN: fire_lizard16 at hotmail dot com
email: parham90 at GMail dot com
"Ollisso" <olli...@fromru.com> wrote in message 
news:op.uxy29woc48v...@ol-n.kyla.fi...
> On Sat, 01 Aug 2009 08:20:23 +0300, "Parham Doustdar" <parha...@gmail.com> 
> wrote:
>
>> Hi there,
>> I've written a counter for my blog, which keeps the count of visitors in 
>> a file. However, when the visitors get too many, it resets to zero. Why?
>>
>> Here's the piece of code:
>>
>> [code]
>> $f = $dir . '/view_counter' .EXT;
>> $fp = fopen($f, "r");
>> $count =fgets($fp, 1024);
>> fclose($fp);
>> $fw = fopen($f, "w");
>> $cnew = $count + 1;
>> $countnew = fputs($fw, $count + 1);
>> return $cnew;
>> [/code]
>>
>> I'm thinking this is caused by two visitors visiting the page at the 
>> same time; but is there a way to fix it, perhaps the reading/writing 
>> parameter?
>>
>> Thanks!
>>
>
> Check:
> http://www.php.net/flock
>
>
> -- 
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ 



--- End Message ---
--- Begin Message ---
Hi,

> ...

You can write a single byte to the file to "increment" the counter,
then to read the count just use filesize(). I believe the a fopen()
mode will handle locking for you. It will result in a slowly growing
file, but space isn't exactly at a premium nowadays, and when the file
gets to a certain size (eg 1 gazillion k) you could use a summary
file.

-- 
Richard Heyes
HTML5 graphing: RGraph - www.rgraph.net (updated 25th July)
Lots of PHP and Javascript code - http://www.phpguru.org

--- End Message ---
--- Begin Message --- On Sun, 02 Aug 2009 07:11:27 +0300, "Parham Doustdar" <parha...@gmail.com> wrote:

Dear Ollisso,
I tried it with FLock() but it still didn't work. This is what I did:
[code]
$fp = fopen($f, "r");
$count =fgets($fp, 1024);
fclose($fp);
$fw = fopen($f, "w");
while (!flock($fw, LOCK_EX))
sleep(1);
$cnew = $count + 1;
$countnew = fputs($fw, $count + 1);
flock($fw, LOCK_UN);
fclose($fw);
[/code]

Am I doing anything wrong here?
Thanks!
Hello,

Actually you should do something like this (according to manual)

$f=fopen("file", "r+");
while(!flock($f, LOCK_EX)) sleep(1);
$count  = fgets($f,1024);
ftruncate($f, 0);
fwrite($f, $count+1);
fclose($f);

Problem in your case: first time you open file for reading, but you don't flock it, so it can read non-writen file.




--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

--- End Message ---
--- Begin Message ---
Now this one always sets the file to one for some reason. I'll reiterate 
just in case there's been a misunderstanding on my part:
[code]
$fp = fopen($f, "r+");
while (!flock($fp, LOCK_EX))
sleep(1);
$count = fgets($fp,filesize($fp));
ftruncate($fp, 0);
fwrite($fp, ($count+1));
flock($fp, LOCK_UN);
fclose($fp);
Thanks a lot again for your help.
-- 
---
Contact info:
Skype: parham-d
MSN: fire_lizard16 at hotmail dot com
email: parham90 at GMail dot com
"Ollisso" <olli...@fromru.com> wrote in message 
news:op.ux03zprl48v...@ol-n.kyla.fi...
> On Sun, 02 Aug 2009 07:11:27 +0300, "Parham Doustdar" <parha...@gmail.com> 
> wrote:
>
>> Dear Ollisso,
>> I tried it with FLock() but it still didn't work. This is what I did:
>> [code]
>> $fp = fopen($f, "r");
>> $count =fgets($fp, 1024);
>> fclose($fp);
>> $fw = fopen($f, "w");
>> while (!flock($fw, LOCK_EX))
>> sleep(1);
>> $cnew = $count + 1;
>> $countnew = fputs($fw, $count + 1);
>> flock($fw, LOCK_UN);
>> fclose($fw);
>> [/code]
>>
>> Am I doing anything wrong here?
>> Thanks!
> Hello,
>
> Actually you should do something like this (according to manual)
>
> $f=fopen("file", "r+");
> while(!flock($f, LOCK_EX)) sleep(1);
> $count = fgets($f,1024);
> ftruncate($f, 0);
> fwrite($f, $count+1);
> fclose($f);
>
> Problem in your case: first time you open file for reading, but you don't 
> flock it, so it can read non-writen file.
>
>
>
>
> -- 
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ 



--- End Message ---
--- Begin Message ---
Dear Richard,
I don't quite know how I can write a bite into a file. I also looked into a 
manual and couldn't find a mention of FLock-ing in the explaination for 
FOpen parameters.
Thanks a lot for your help.

-- 
---
Contact info:
Skype: parham-d
MSN: fire_lizard16 at hotmail dot com
email: parham90 at GMail dot com
"Richard Heyes" <rich...@php.net> wrote in message 
news:af8726440908020221r22e1efb3g321ba4140bfa4...@mail.gmail.com...
> Hi,
>
>> ...
>
> You can write a single byte to the file to "increment" the counter,
> then to read the count just use filesize(). I believe the a fopen()
> mode will handle locking for you. It will result in a slowly growing
> file, but space isn't exactly at a premium nowadays, and when the file
> gets to a certain size (eg 1 gazillion k) you could use a summary
> file.
>
> -- 
> Richard Heyes
> HTML5 graphing: RGraph - www.rgraph.net (updated 25th July)
> Lots of PHP and Javascript code - http://www.phpguru.org 



--- End Message ---
--- Begin Message ---
Hi,

Does anyone has any idea how to create notification system with
combination of php, mysql and javascript. It should be something
similar to facebook notification system (when someone make some action
it should be automatically reported to other people on system through
pop-up menu or something like that). I just need some basic idea how
to start or if someone has some example it would be perfect.

Thanks,
Dusan

-- 
made by Dusan

--- End Message ---

Reply via email to