PHP file operation-add data from other files to this file

不言
Release: 2023-03-23 08:34:02
Original
1456 people have browsed it

The content of this article is php file operation-add the data of other files to this file, now share it with everyone, friends in need can refer to it

GitHub source code
In fact, our The program writes answers based on the following questions:

There are two text files A.txt B.txt
A.txt 30 million lines, userid is unique, userid and username are separated by spaces, as shown below:
userid username
1 yi yi
2 er er
3 san
… …
B.txt 30 million lines, userid is unique, userid and realname are separated by spaces, as shown below:
userid realname
1 ##userid realname
1 Go to the third column of B.txt and give the time complexity.

In our program, by default, the row data of the two files are in one-to-one correspondence, that is, the nth row of data in A corresponds to the nth row of data in B. In this way, the time complexity of our program is O(n).

But in actual operation, there will definitely be a situation where the data lines of the two files do not correspond one to one. In this case, I only think of the simplest operation with a time complexity of O(n^2) , I don’t know if there is a better algorithm to solve this problem.

If the file is read out and constructed into a data structure whose query is O(1) or O(logn), the complexity should become O(n) or O(nlogn), but when encountering a file Too large, how to deal with the situation when the constructed data structure is too large, learn from the practice of database b-tree index?


         
Copy after login
Copy after login

GitHub Source Code

In fact, our program is based on the following issues Written answer:

There are two text files A.txt B.txt
A.txt 30 million lines, userid is unique, userid and username are separated by spaces, as follows:

userid username

1 yi yi
2 er
3 san
… …
B.txt 30 million lines, userid is unique, userid and realname are separated by spaces, as shown below:
userid realname
1 一
2  二
3   三
… …
Please write a piece of code to find the username corresponding to the userid in B.txt in A.txt and fill it in B.txt The third column gives the time complexity.

In our program, by default, the row data of the two files are in one-to-one correspondence, that is, the nth row of data in A corresponds to the nth row of data in B. In this way, the time complexity of our program is O(n).

But in actual operation, there will definitely be a situation where the data lines of the two files do not correspond one to one. In this case, I only think of the simplest operation with a time complexity of O(n^2) , I don’t know if there is a better algorithm to solve this problem.

If the file is read out and constructed into a data structure whose query is O(1) or O(logn), the complexity should become O(n) or O(nlogn), but when encountering a file Too large, how to deal with the situation when the constructed data structure is too large, learn from the practice of database b-tree index?


         
Copy after login
Copy after login

Related recommendations:

Get the directory for PHP file operations Files and methods of calculating relative paths

patch files PHP file operations to implement code sharing

The above is the detailed content of PHP file operation-add data from other files to this file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!