Asked  2 Years ago    Answers:  5   Viewed   53 times

I have a script which, each time is called, gets the first line of a file. Each line is known to be exactly of the same length (32 alphanumeric chars) and terminates with "rn". After getting the first line, the script removes it.

This is done in this way:

$contents = file_get_contents($file));
$first_line = substr($contents, 0, 32);
file_put_contents($file, substr($contents, 32 + 2)); //+2 because we remove also the rn

Obviously it works, but I was wondering whether there is a smarter (or more efficient) way to do this?

In my simple solution I basically read and rewrite the entire file just to take and remove the first line.

 Answers

1

There is no more efficient way to do this other than rewriting the file.

Tuesday, December 13, 2022
2

It happens because PHP CS Fixer modifies the file, as @LazyOne said, but you have empty value of "Output paths to refresh" so IDE cannot know about these changes.

Set the value of "Output paths to refresh" to $FileName$ - same as in arguments - to make PhpStorm aware about the changes (it depends on the "Working directory" value which has been set in Other Options - if it is set to $FileDir$ then you don't need to mention it in the paths to refresh).

Wednesday, December 21, 2022
 
1

I'd guess that you are on windows and that you have I: mapped to a share such as \server2files ...

If so, that's your problem. These mappings are only avaialble to the current users (eg, the admin account), not to the IUSR account that your php is probably running as (assuming IIS). Solution, don't use mappings, instead use the full 'unc' path name, ie '\serversharefolderfile.ext', also remember that the IUSR account will need access to these shares/folders/files

Monday, October 3, 2022
4

docs for io module

with open(fname, 'rb') as fh:
    first = next(fh).decode()

    fh.seek(-1024, 2)
    last = fh.readlines()[-1].decode()

The variable value here is 1024: it represents the average string length. I choose 1024 only for example. If you have an estimate of average line length you could just use that value times 2.

Since you have no idea whatsoever about the possible upper bound for the line length, the obvious solution would be to loop over the file:

for line in fh:
    pass
last = line

You don't need to bother with the binary flag you could just use open(fname).

ETA: Since you have many files to work on, you could create a sample of couple of dozens of files using random.sample and run this code on them to determine length of last line. With an a priori large value of the position shift (let say 1 MB). This will help you to estimate the value for the full run.

Saturday, October 1, 2022
 
1

Looking at the de-compiled code for Contains(), it just calls IndexOf() with StringComparison.Ordinal, so I'd say IndexOf() is most efficient (by a very small hair) i if used in the same way (Ordinal) since it has one less method call, but Contains() is more readable and therefore more maintainable...

public bool Contains(string value)
{
    return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}

As in all things, I'd go with what's more readable and maintainable then splitting hairs on performance. Only do micro-optimization when you know there's a bottleneck at this point.

UPDATE: Over 1,000,000 iterations:

  • Contains(value) - took 130ms
  • IndexOf(value, StringComparison.Ordinal) - took 128 ms

So as you can see, very, very NEAR same. Once again, go with what's more maintainable.

UPDATE 2: If your code is always a single char (not a 1-char string), IndexOf() is faster:

  • Contains(char value) - took 94 ms
  • IndexOf(char value) - took 16 ms

If you know your char codes are always a single char, it is about an order of magnitude faster to use IndexOf() with a char argument.

This is because Contains(char value) is an extension method off of IEnumerable<T> and not a first class method of string.

But once again ~100 ms over 1,000,000 iterations is really, truly, quite negligible.

Monday, September 19, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share

Browse Other Code Languages