What is the most efficient way to find and replace something inside a file?

Currently I am trying to create a script that replaces duplicate objects with its duplicate. After finding all duplicate objects I try to replace them. Currently it takes over several hundred hours to complete, and freezes the unity editor for this duration of time.

The code I am using is as following (the void gets called once)

(as some of the code lines fell outside of the code bounds here on the site I posted it on pastebin instead.) public void CombineDependencys() { //A while loop for all - Pastebin.com

Would there be a faster way to check all these lines for the GUID in use and replace this? preferably without freezing the unity editor

Edit

Following some hints I already improved the performance slightly by

  1. Calling 2/3 AssetDatabase calls outside of the loop as this might be intensive call
  2. Using a string.replace instead of a regex, which seems to be less heavy then a regex call

even with these changes the performance did not increase enough to make a significant difference though.

to find file to replace, try getfiles and msdn commands as in this code:

	texture1 = GOcube4.renderer.material.mainTexture;
	
	var retrievepath : String = AssetDatabase.GetAssetPath(texture1); //try to find path of texture
	
	// if (retrievepath == null)
	// {
	var title : String = GOcube4.renderer.material.mainTexture.name;
	
	var path1 = System.IO.Directory.GetFiles(Application.dataPath, title+".*", System.IO.SearchOption.AllDirectories);

	for (path1i  in path1) 
	{
	var last3chars : String = path1i.Substring(path1i.Length - 3);

	if (last3chars != "eta" && last3chars != "mat"  ) 
	{retrievepath = path1i ; }
	}
	if (retrievepath.StartsWith(Application.dataPath)) {
		retrievepath =  "Assets" + retrievepath.Substring(Application.dataPath.Length);
	}
	
	
	//retrievepath = retrievepath.Replace(  "\\" ,  "/"  );
	print(retrievepath);

	

	var  tImporter : TextureImporter = AssetImporter.GetAtPath( retrievepath ) as TextureImporter;
	if ( tImporter == null ) print( "sorry i couldnt find texture named " + title + " please find picture manually and make it read enabled "); 

if( tImporter != null ) {
    //tImporter.mipmapEnabled = ...;
    tImporter.isReadable = true ;
		//tImporter.textureFormat = TextureImporterFormat.RGB24 ;
   // tImporter.maxTextureSize = ...;
    AssetDatabase.ImportAsset( retrievepath, ImportAssetOptions.ForceUpdate );  
		print ("completed")			;
}