开发者

Which is quicker; Comparing text against an array, or an external file?

开发者 https://www.devze.com 2023-01-11 17:46 出处:网络
I have 开发者_如何学Goto implement a \"bad words\" filter on my website, which is a classifieds website.

I have 开发者_如何学Goto implement a "bad words" filter on my website, which is a classifieds website.

I have a big list of "bad words" but don't know which method is best to compare the user inputs to.

In my case, a textarea inside a form, needs to be checked for "bad words".

   <form name="test" action="test.php" method="post">

Inside test.php I fetch the textarea, and need to compare it...

My Q is, would you compare it to an external text-file with bad words, or an array with bad-words?

The array I think is better, so I don't need any external functions etc, but I need to be sure...

What do you think?

Thanks


An array/list would be quicker overall if you are checking many words. You only have to read the file once and then each check will be against the list.

However, in your application (assuming you want to go ahead despite the pitfalls) it might be better to read the file only when you need to. That way the file could be updated while the application is still running and you wouldn't have to stop and restart the application or call some admin function to reparse the file.

The delay in submission probably won't be noticed by the user anyway. Though using a caching algorithm to see if the file has changed would minimise this.


Independant of the programming language you are using, I think using in memory arrays for comparison would always be a good and efficient solution considering that its a list of bad words and wouldnt grow really huge.


Doing it in an array will definitely be faster as you are not reading from disk. What many user do is store the bad words in the database or a file and read them into a cache (such as memcache or APC) and then look to see if they are in the cache first and if they are not, read from file and then put them into the cache. This is a good approach that is flexible and fast.

0

精彩评论

暂无评论...
验证码 换一张
取 消