The procedure is as follows:
public static string English-Chinese (string english, translation results one or more)
{
string English-Chinese dictionary = .Dictionary directory + "haou_dict.xml";
try
{
if ((English-Chinese Dictionary))
{
XDocument dictionary = (English-Chinese dictionary);
var query = from p in ("Name")
where () == ()
select ("Content");
if (() == null)
{
return null;
}
else// can be translated
{
if (one or more == translation result. multiple)
{
return ().();
}
else if (one or more == translation result. one)
{
return Get the first Chinese character (().Value);
}
throw new Exception("Parameter error!");
}
}
else
{
throw new Exception("Dictionary file does not exist!");
}
}
catch (Exception)
{
throw new Exception("The dictionary file error!");
}
}
I analyzed it, and the reason is that the English-Chinese dictionary in the yellow bar is loaded every time I call this method. If it is executed 2,000 times, it will be loaded 2,000 times. Naturally, the speed is slow. Is there any way to load it only once when the program is running? I remember that there is a singleton factory pattern in the design pattern I have seen in Shangxuetang, which uses static variables, which seems to be able to solve it. Example verification:
Define a class first:
class translation{
public static XDocument English-Chinese dictionary content = English-Chinese dictionary initialization();
public XDocument constructs the content of English-Chinese dictionary;
public translation()
{
Construct the content of English-Chinese dictionary = English-Chinese dictionary initialization();
}
public static XDocument English-Chinese dictionary initialization()
{
if ((English and Chinese dictionary file))
{
return (English-Chinese dictionary document);
}
else
{
throw new Exception("English-Chinese dictionary file does not exist!");
}
}
}
Main program:
var one = translation. English-Chinese dictionary content;
var two = translation.English-Chinese dictionary content;
if (one==two)
{
("same");
}
else
{
("different");
}
one = new Translation(). Construct the content of English-Chinese dictionary;
two = new translation(). Construct the content of English-Chinese dictionary;
if (one == two)
{
("same");
}
else
{
("different");
}
result:
Whether it is using static translation, English-Chinese dictionary content, or new translation(). Constructing English-Chinese dictionary content, they call English-Chinese dictionary initialization(), but the values in static variables will only be initialized once. Each subsequent access is the value processed last time, so the result displayed for the first time is the same, that is, the value of one and two is actually one, and the second two access is actually the value after the first one is initialized; and using the class constructor to initialize the variable will be repeatedly initialized every time, and the result will naturally be different.
For example (the following content is reprinted):
class Program
{
static void Main(string[] args)
{//Output an undefined static variable, the result is 0; it also shows that the variable system that has not assigned an initial value in C# is automatically assigned to 0
();
//Access methods of static variables (class name.static variable name), and static variables can also be operated externally. It can be seen that static variables are not mysterious;
= 5;
//Output 5
();
//You can also use the constructor to adjust the initial value of the static variable, haha
sort sortTest = new sort();
//The assignment in the output constructor is 3;
();
}
}
class sort
{
public static int i;
public sort()
{
i = 3;
}
}
Summary: When accessing static variables inside a class, just use the static variable name directly, and do not use (class name. static variable name) to access it in this way.
In addition to static variables, there are also static class instances and static methods. However, the usages are similar;
For example: public static void myFun(){} //static method
private static Random MyRandom=new Random(); // Static class instance
The reason why it is sometimes declared as a private static variable is to make it initialized only once. This saves memory space
But I want it to be inaccessible externally, so I can use the private access qualifier to get it done.
Private static: safe and space-saving.
Example: If you want to generate a set of random numbers at each time of instantiation of the class, but generating random numbers requires a class, that is, Random. This class is not a static class. It needs to generate instances and use the generated instances to generate random numbers. However, if a Random instance is generated every time the class is instantiated, then the memory space is a huge waste, so you can use:
private static Random MyRandom=new Random();
In this way, every time the class is instantiated, the same Random instance MyRandom will be used to generate random numbers
Copy the codeThe code is as follows:
public static string English-Chinese (string english, translation results one or more)
{
string English-Chinese dictionary = .Dictionary directory + "haou_dict.xml";
try
{
if ((English-Chinese Dictionary))
{
XDocument dictionary = (English-Chinese dictionary);
var query = from p in ("Name")
where () == ()
select ("Content");
if (() == null)
{
return null;
}
else// can be translated
{
if (one or more == translation result. multiple)
{
return ().();
}
else if (one or more == translation result. one)
{
return Get the first Chinese character (().Value);
}
throw new Exception("Parameter error!");
}
}
else
{
throw new Exception("Dictionary file does not exist!");
}
}
catch (Exception)
{
throw new Exception("The dictionary file error!");
}
}
I analyzed it, and the reason is that the English-Chinese dictionary in the yellow bar is loaded every time I call this method. If it is executed 2,000 times, it will be loaded 2,000 times. Naturally, the speed is slow. Is there any way to load it only once when the program is running? I remember that there is a singleton factory pattern in the design pattern I have seen in Shangxuetang, which uses static variables, which seems to be able to solve it. Example verification:
Define a class first:
Copy the codeThe code is as follows:
class translation{
public static XDocument English-Chinese dictionary content = English-Chinese dictionary initialization();
public XDocument constructs the content of English-Chinese dictionary;
public translation()
{
Construct the content of English-Chinese dictionary = English-Chinese dictionary initialization();
}
public static XDocument English-Chinese dictionary initialization()
{
if ((English and Chinese dictionary file))
{
return (English-Chinese dictionary document);
}
else
{
throw new Exception("English-Chinese dictionary file does not exist!");
}
}
}
Main program:
Copy the codeThe code is as follows:
var one = translation. English-Chinese dictionary content;
var two = translation.English-Chinese dictionary content;
if (one==two)
{
("same");
}
else
{
("different");
}
one = new Translation(). Construct the content of English-Chinese dictionary;
two = new translation(). Construct the content of English-Chinese dictionary;
if (one == two)
{
("same");
}
else
{
("different");
}
result:
The analysis is as follows:
Whether it is using static translation, English-Chinese dictionary content, or new translation(). Constructing English-Chinese dictionary content, they call English-Chinese dictionary initialization(), but the values in static variables will only be initialized once. Each subsequent access is the value processed last time, so the result displayed for the first time is the same, that is, the value of one and two is actually one, and the second two access is actually the value after the first one is initialized; and using the class constructor to initialize the variable will be repeatedly initialized every time, and the result will naturally be different.
For example (the following content is reprinted):
Copy the codeThe code is as follows:
class Program
{
static void Main(string[] args)
{//Output an undefined static variable, the result is 0; it also shows that the variable system that has not assigned an initial value in C# is automatically assigned to 0
();
//Access methods of static variables (class name.static variable name), and static variables can also be operated externally. It can be seen that static variables are not mysterious;
= 5;
//Output 5
();
//You can also use the constructor to adjust the initial value of the static variable, haha
sort sortTest = new sort();
//The assignment in the output constructor is 3;
();
}
}
class sort
{
public static int i;
public sort()
{
i = 3;
}
}
Summary: When accessing static variables inside a class, just use the static variable name directly, and do not use (class name. static variable name) to access it in this way.
In addition to static variables, there are also static class instances and static methods. However, the usages are similar;
For example: public static void myFun(){} //static method
private static Random MyRandom=new Random(); // Static class instance
The reason why it is sometimes declared as a private static variable is to make it initialized only once. This saves memory space
But I want it to be inaccessible externally, so I can use the private access qualifier to get it done.
Private static: safe and space-saving.
Example: If you want to generate a set of random numbers at each time of instantiation of the class, but generating random numbers requires a class, that is, Random. This class is not a static class. It needs to generate instances and use the generated instances to generate random numbers. However, if a Random instance is generated every time the class is instantiated, then the memory space is a huge waste, so you can use:
private static Random MyRandom=new Random();
In this way, every time the class is instantiated, the same Random instance MyRandom will be used to generate random numbers