PDA

View Full Version : How many lines of code do you have?



Floydian
04-20-2008, 08:09 PM
<?php # Count total line numbers

/*
Apply a recursive directory/file reading function
that will total up how many lines all the .php and .js files
in the directory, and sub-directories starting from where this file is placed.
*/

function line_counter($dir) {
// echo "Dir: $dir ----------- <hr> ";
$files = scandir($dir);

static $count = 0;

foreach ($files as $key =>$filename) {
if (in_array($key, array(0,1)) or in_array($filename, array('purifier'))) {
continue;
}
if (is_dir($dir.$filename)) {
$new_path = $dir . $filename . '/';
line_counter($new_path);
} else {
$link_name = explode('.',$filename);
if (!isset($link_name[1]) or !in_array($link_name[1], array('php', 'js')) ) {

} else {
$count += count(file($dir.$filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
}
// echo "$dir.$filename<hr>";
}


}

return $count;
} // End of line_counter() function.

$count = line_counter('./');
echo "<h1>final tally: $count</h1>";
?>


This code will recursively search through directories starting from where this script is placed, and count up how many lines of code are contained in your files.

Uncomment this line, if you want to see all the directories searched.


// echo "Dir: $dir ----------- <hr> ";

Uncomment this line of you want to see all the files that were included in the count.

// echo "$dir.$filename<hr>";

This next line serves two purposes:
1. skip the . and .. which on my system are always the elements in the array with keys of 0 and 1. This is system dependant. You'll have to view the array using var dump or print_r on the $files array to see where it is on your system.

2. The second part filters out certain directories so they aren't included. I did not want third party libraries included in the count.


if (in_array($key, array(0,1)) or in_array($filename, array('purifier'))) {


This last part filters out file names that don't have an extension, or that don't have an extension we want to count. I chose to only count php and js files.


if (!isset($link_name[1]) or !in_array($link_name[1], array('php', 'js')) ) {

My count:

final tally: 39672

Edit:

set_time_limit(0);
was removed from the code, it's unnecessary.

Will
04-20-2008, 09:11 PM
Final Tally: 1,317,167

I must have a lot of junk....

Not sure why you need to set the time limit; mine only took a few seconds.

EDIT: I tried to run on my entire directory but it ran out of memory...

topmorpg
04-20-2008, 09:25 PM
is this only for mc codes as scandir was not a valid function in my server for a different type of site i was testing it on.

Anonymous
04-20-2008, 09:27 PM
Hmm, current project: 917,555
(DBS/MCcode kinda, but 100% fresh code)

Oops - forgot the CSS and JavaScript
1,452,300

Although I will own up and say I didn't write all the JavaScript ;)

EDIT Hmm, seems I may have been a trifle optimistic here... The system I use for generating pages itself writes files which I added in by mistake. Guess I'll have to drop my initial figure by rather a lot

Floydian
04-20-2008, 10:21 PM
It might be php >= 5 dependant. There's nothing this code depends on as far as php includes. It is being run on php 5.2.5, apache 1.3, and windows xp. There's no mysql involved.

One thing about file systems, different operating systems will have to be compensated for. But if you have my set (perhaps apache version doesn't matter) it should run out of the box, if your php is up to date.

Mccodes? I dun see how it's mccodes (kinda) lol

But yeah, Nyna you are right, it's 100% my code ;)


Edit:
Oh, and Will you're absolutely right about the time setting, I should have taken it out of there. I'm sure you can identify with making recursive functions and thinking, well, it took too long to execute, so perhaps I need to allow more time. Then ya find out that your code was just wrong. lol

So yeah, it's a remnant. I'll edit it out.

topmorpg
04-20-2008, 10:39 PM
yea scandir is for php5

http://www.php.net/manual/en/function.scandir.php

Spudinski
04-21-2008, 03:52 PM
I made something like this a while back, just to show off... d:
But mine takes long in my directories(the big ones), so I used yours, with only one problem to it.

When it goes on for a bit, at about the 6th directory, it starts to recount all the directories, here is an example:

Dir: ./blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../PPW/ -----------
Dir: ./blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../PPW/cache/ -----------
Dir: ./blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../PPW/net/ -----------

I don't know why this is doing this, but I had to add the following to your code to fix it(simple but works):


// original statement
if (is_dir($dir.$filename)) {
$new_path = $dir . $filename . '/';
line_counter($new_path);
}


// my alteration
if (is_dir($dir.$filename) && !strstr($new_path, '/../')) {
$new_path = $dir . $filename . '/';
line_counter($new_path);
}

final tally: 1,870,155
I have allot of junk though..

Decepti0n
04-21-2008, 04:36 PM
Damn, where's everybody getting these numbers from? The most I managed on a game I built was about 30,000. Maybe we have different definitions of 'a lot of junk'.

edit; made it to 500k, by scanning my entire www directory ... which half of that I didn't even make I guess. Seriously, any examples of a file that would even make a dent in 1mil rows? :\

Floydian
04-21-2008, 06:02 PM
Well, while I don't call into question anyone's claims (and I don't mean to suggest Decepti0n does either) I do have a lingering question in my mind as to how long it would take to write a million lines of code.

So I did some calculations, because a number like 1 million is hard to grapple wholesale.

Assuming an 8 hour day of work, 7 days a week, and writting code at a rate of 1 line every second solid during those 8 hours of every day, then it would take 34.7 days to write 1 million lines of code.

If we assume that each line takes 30 seconds to write, and again assuming a 7 day work week, 8 hour work day, and a solid one line for every 30 seconds on the job, it would take 1041.7 days to write 1 million lines of code or 2.85 years (again, working every day, 8 hours per day, no breaks, and 1 line of code every 30 seconds.)

It gets even crazier if we add in some other things. Let's say 20% of your time is spent thinking and not writing at all. That leaves 6.4 hours left. Then we'll take out a 30 minute lunch break. 5.9 hours. Then we'll assume a 5 day work week. (this adds in 2 days, for every 5 days in the final total) and if the final total goes over a year (which we know it will), we'll add in two weeks vacation time. lol

With this scenario, we'll assume that the coder is coding at a solid rate of 1 line every 30 seconds while he/she is actively involved in coding.

1412.43 Days (before adding in weekends)

1977.40 Days (with weekends) That's 5.4 years

Five vacations, two weeks each is 70 days.

The grand total is 2047.40 days to write one million lines of code, working a solid 5.9 hours per day, writing one line of code every 30 seconds, taking two days off per week, taking a two week vacation every year.


Edit: I incorectly went from 6.4 hours to 6.1 hours adding in a lunch break. This of course resulted in faster coding times. The new calculation uses a 5.9 hour work day which is 30 minutes off of 6.4 hours. ;)

Anonymous
04-21-2008, 06:18 PM
That's a very good point, however I'm sure you use code generators from time to time...

I have large blocks of fairly similar code that is often in a simple format: text, csv, xml etc., that is used to generate the main php files which then need little editing to be up and running.

There's also the case of cut 'n' paste. A lot of my front end script are very similar - a large try { } catch { } block, which I freely admit to writing ... once ;)

It's difficult to get a true representation of what we write - after all - look at my own site - if you view source on any page you will see one line. In fact each page is multiple lines (hence me counting the templates), but rendered as one.

Haunted Dawg
04-21-2008, 06:21 PM
Was alterating this...



<?php # Count total line numbers
/*
Apply a recursive directory/file reading function
that will total up how many lines all the .php and .js files
in the directory, and sub-directories starting from where this file is placed.
*/
echo '<title>Line Counter</title>';
function line_counter($dir)
{
//echo "Dir: $dir ----------- <hr>"; //take away the //if you want to display all the directories searched.
$files = scandir($dir);
static $count = 0;
foreach ($files as $key => $filename)
{
if (in_array($key, array(0,1)) or in_array($filename, array('purifier')))
{
continue;
}
if (is_dir($dir.$filename))
{
$new_path = $dir . $filename . '/';
line_counter($new_path);
}
else
{
$link_name = explode('.',$filename);
if (!isset($link_name[1]) or !in_array($link_name[1], array('php', 'js')))
{
//Do something...
}
else
{
$count += count(file($dir.$filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
}
//echo "$dir.$filename<hr>"; //Remove the //if you want it to display all the files that were included in the count.
}
}
return $count;
} // End of line_counter() function.
$count = line_counter("./");
echo '<h1>Final Tally: '.number_format($count).'</h1>';
?>


I used that and got this on www.finalkillah.com/zender.php (http://www.finalkillah.com/zender.php)



Final Tally: 18,095

-Matt-
04-21-2008, 06:59 PM
Fatal error: Call to undefined function: scandir() in /home/amazonba/public_html/code.php on line 11

Anonymous
04-21-2008, 07:01 PM
yea scandir is for php5

http://www.php.net/manual/en/function.scandir.php

Floydian
04-21-2008, 07:50 PM
...I'm sure you use code generators from time to time...
....
There's also the case of cut 'n' paste. ....
...

[SNIPPED by floydian]

Very true. And I don't fault anyone for doing that :D

Of course every instance of copying lines can't be accounted for with this script.

I skipped over the htmlpurifier code as there is quite a bit of code there that I didn't write, and although it could be included in the count, I wanted to know how many lines I wrote.

Code generators, nope, can't say that I've used that. I do use an ajax javascript function over and over again. That function was not orginally writen by me. Every one of them has some amount of editing. I did include all of those since there is quite a bit of javascript that I did write, and there's no easy way to eliminate them.

In the end, my total of 39k lines of code is an overestimate of how many lines I've written. It would be interesting to include the html purifier and any other 3rd party code just to see how many lines the entire project is.