user-0 提问: 数值计算的程序跑的很慢是怎么回事啊？

回复:
user: user-1
content: 原因可能是多个，建议贴出相关的代码，然后具体问题具体分析

user: user-2
content: 你换个超级计算机

user: user-3
content: 大量的循环计算，频繁的IO操作都有可能，你发代码上来才好分析。

user: user-4
content: 可能很多原因，比如算法不好，循环太多

user: user-3
content: 代码还挺复杂，这么多循环嵌循环的，慢是正常的了。你可以在关键位置输出一下时间，统计一下最终哪一部分耗费时间最多，再考虑优化。  如果算法没多少优化空间了，考虑一下多线程吧。

user: user-0
content: 还有我之前发的那个帖子，你给我回复了，两个程序的区别是什么？谢谢

user: user-1
content: 逻辑看着好复杂。 另外，有这么多循环（while（1）和for）怎么能不跑的慢； 建议优化一下程序吧； 不要把所有功能代码都写到main函数里，这样很不容易理解代码逻辑，另外，代码看着也很臃肿； 变量一下子定义这么多，如果变量能够复用的就要复用。比如循环变量，就用i, j, k就好了，基本上就够了。

user: user-0
content: #include <stdio.h> #include <stdlib.h> #include <math.h> #define r    3.14159265358979 #define pi  3.14159265358979 #define ex  0.000001 #define h   0.01 #define a   0 #define b   300 #define n   4 #define um 2.0 #define uc  0.05 #define uk  6.6 #define q  0.2 #define f0  0.8 #define uv  0.1 #define us  0.1 #define td  0.05 #define fu1 1.0               //摩擦力1，定值fu1=1.0 #define fu2 1 / (1 + um)      //摩擦力2，fu1 > fu2       /************Coulomb model************/ double fp1(double k)   //fp1=sign， k=c[3] { 	if (k > ex) 		return fu1; 	else if (k < -ex) 		return -fu1; 	else 		return 0.0; }  double fp2(double s, double z)    //s = c[1], z = c[3] { 	if (s - z > ex) 		return fu2; 	else if (s - z < -ex) 		return -fu2; 	else 		return 0.0; }  dun(double f[n], double t, double w1, double r1, int b1, int b2, int b3, int b4, int b5, int b6) { 	double g[n]; int i; 	g[0] = f[1]; 	g[1] = (b1 + b2)*(f0*cos(w1*t + r1) + (f[2] - f[0]) + 2 * uc*(f[3] - f[1]) - fp2(f[1], f[3])) 		+ (b3 + b6)*((f0*cos(w1*t + r1) - fp1(f[3])) / (1 + um)) 		+ (b4 + b5)*(f0*cos(w1*t + r1) + (f[2] - f[0]) + 2 * uc*(f[3] - f[1]) + uk*(f[2] - f[0] + q) - fp2(f[1], f[3])); 	g[2] = (b2 + b5)*f[3] + (b3 + b6)*f[1]; 	g[3] = b2*(((f[0] - f[2]) + 2 * uc*(f[1] - f[3]) + fp2(f[1], f[3]) - fp1(f[3])) / um) 		+ (b3 + b6)*((f0*cos(w1*t + r1) - fp1(f[3])) / (1 + um)) 		+ b5*(((f[0] - f[2]) + 2 * uc*(f[1] - f[3]) + uk*(f[0] - f[2] - q) + fp2(f[1], f[3]) - fp1(f[3])) / um); 	for (i = 0; i<n; i++) 		f[i] = g[i]; }

user: user-0
content: main() { 	int i, m, j = 0, b1, b2, b3, b4, b5, b6; 	double k[n], c[n], sum[n], e2[n], h1, h2, x, x1, x2, y1, y2, y3, w1, r1 = r;  	FILE* fp1; 	fp1 = fopen("模型二-相图.txt", "w");  	for (i = 0; i<n; ++i) 		c[i] = 0;  	b1 = 1; b2 = 0; b3 = 0; b4 = 0; b5 = 0; b6 = 0;  	for (w1 = 0.6; w1 <= 0.6; w1 = w1 + 0.001) 	{ 		printf("%lf\n", w1); 		x = x1 = y2 = y3 = a; 		h1 = h; 		h2 = h / 2;  		m = 0;  		while (w1*x <= 10000) 		{ 			//printf("%lf\n", w1*x); 			for (j = 0; j < n; j++) 			{ 				k[j] = c[j]; e2[j] = c[j]; 			} 			y1 = x; 			y3 = y2; 			if (h1 < 0.005) 				h1 = h;  			h2 = h1 / 2; 			lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 			lk(e2, h2, x, w1, r1, b1, b2, b3, b4, b5, b6); 			x1 = x + h2; 			lk(e2, h2, x1, w1, r1, b1, b2, b3, b4, b5, b6);  			while (norm1(e2, c, ex)) 			{ 				h1 = h2; 				h2 = h2 / 2; 				for (j = 0; j < n; j++) 				{ 					c[j] = k[j]; e2[j] = k[j]; 				} 				lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 				lk(e2, h2, x, w1, r1, b1, b2, b3, b4, b5, b6); 				x1 = x + h2; 				lk(e2, h2, x1, w1, r1, b1, b2, b3, b4, b5, b6); 			}  			for (i = 0; i<n; i++) 				c[i] = (16 * e2[i] - c[i]) / 15.0;  			x = x + h1; 			y2 = y2 + h1; 			/**************************************************/ 			if ((k[0] - k[2]) - q < -ex && (c[0] - c[2]) - q > ex) 			{ 				x = y1;  y2 = y3; 				for (i = 0; i < n; i++) 					c[i] = k[i];  				while (1) 				{ 					lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					while ((c[0] - c[2]) - q > ex) 					{ 						h1 = h1 / 2; 						for (i = 0; i < n; i++) 							c[i] = k[i]; 						lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					} 					if ((c[0] - c[2]) - q >= -ex) 					{ 						x = x + h1; y2 = y2 + h1; break; 					} 					for (i = 0; i < n; i++) 						k[i] = c[i]; 					x = x + h1; 					y2 = y2 + h1; 				} 			} 			/**************************************************/ 			if ((k[0] - k[2]) - q > ex && (c[0] - c[2]) - q < -ex) 			{ 				x = y1;  y2 = y3; 				for (i = 0; i < n; i++) 					c[i] = k[i];  				while (1) 				{ 					lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					while ((c[0] - c[2]) - q < -ex) 					{ 						h1 = h1 / 2; 						for (i = 0; i < n; i++) 							c[i] = k[i]; 						lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					} 					if ((c[0] - c[2]) - q <= ex) 					{ 						x = x + h1; y2 = y2 + h1; break; 					} 					for (i = 0; i < n; i++) 						k[i] = c[i]; 					x = x + h1; 					y2 = y2 + h1; 				} 			} 			/**************************************************/ 			if (k[3] > ex && c[3] < -ex) 			{ 				x = y1;  y2 = y3; 				for (i = 0; i < n; i++) 					c[i] = k[i];  				while (1) 				{ 					lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					while (c[3] < -ex) 					{ 						h1 = h1 / 2; 						for (i = 0; i < n; i++) 							c[i] = k[i]; 						lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					} 					if (c[3] <= ex) 					{ 						x = x + h1; y2 = y2 + h1; break; 					} 					for (i = 0; i < n; i++) 						k[i] = c[i]; 					x = x + h1; 					y2 = y2 + h1; 				} 			} 			/**************************************************/ 			if (k[3] < -ex && c[3] > ex) 			{ 				x = y1;  y2 = y3; 				for (i = 0; i < n; i++) 					c[i] = k[i];  				while (1) 				{ 					lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					while (c[3] > ex) 					{ 						h1 = h1 / 2; 						for (i = 0; i < n; i++) 							c[i] = k[i]; 						lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					} 					if (c[3] >= -ex) 					{ 						x = x + h1; y2 = y2 + h1; break; 					} 					for (i = 0; i < n; i++) 						k[i] = c[i]; 					x = x + h1; 					y2 = y2 + h1; 				} 			} 			/**************************************************/ 			if (k[1] - k[3] > ex && c[1] - c[3] < -ex) 			{ 				x = y1;  y2 = y3; 				for (i = 0; i < n; i++) 					c[i] = k[i];  				while (1) 				{ 					lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					while (c[1] - c[3] < -ex) 					{ 						h1 = h1 / 2; 						for (i = 0; i < n; i++) 							c[i] = k[i]; 						lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					} 					if (c[1] - c[3] <= ex) 					{ 						x = x + h1; y2 = y2 + h1; break; 					} 					for (i = 0; i < n; i++) 						k[i] = c[i]; 					x = x + h1; 					y2 = y2 + h1; 				} 			} 			/**************************************************/ 			if (k[1] - k[3] < -ex && c[1] - c[3] > ex) 			{ 				x = y1;  y2 = y3; 				for (i = 0; i < n; i++) 					c[i] = k[i];  				while (1) 				{ 					lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					while (c[1] - c[3] > ex) 					{ 						h1 = h1 / 2; 						for (i = 0; i < n; i++) 							c[i] = k[i]; 						lk(c, h1, x, w1, r1, b1, b2, b3, b4, b5, b6); 					} 					if (c[1] - c[3] >= -ex) 					{ 						x = x + h1; y2 = y2 + h1; break; 					} 					for (i = 0; i < n; i++) 						k[i] = c[i]; 					x = x + h1; 					y2 = y2 + h1; 				} 			} 			/***********Poincare截面***********/ 			if ((c[0] - c[2]) - q >= -ex && (c[0] - c[2]) - q <= ex && c[1] - c[3] > ex) 			{ 				m = m + 1; 				if (m == 200) 				{ 					r1 = w1*x + r1; 					while (r1 >= 2 * pi) 						r1 = r1 - 2 * pi; 					x = a; 					c[0] = c[0] - c[2]; 					c[2] = 0.0; 				} 			} 			/***********(1)m1与plate分别自由振动，capsule静止***********/ 			/***********(2)m1与plate分别自由振动，capsule运动***********/ 			if ((c[0] - c[2]) - q < -ex) 			{ 				if (fabs(c[3]) > ex) 				{ 					if (fabs(c[1] - c[3]) > ex) 					{ 						b1 = 0; 						b2 = 1; 						b3 = 0; 						b4 = 0; 						b5 = 0; 						b6 = 0; 					} 					else 					{ 						if (fabs(f0*cos(w1*x + r1) + (c[2] - c[0]) + 2 * uc*(c[3] - c[1])) <= fu2) 						{ 							b1 = 0; 							b2 = 0; 							b3 = 1; 							b4 = 0; 							b5 = 0; 							b6 = 0; 							c[1] = c[3]; 						} 						else 						{ 							b1 = 0; 							b2 = 1; 							b3 = 0; 							b4 = 0; 							b5 = 0; 							b6 = 0; 							c[1] = c[3]; 						} 					} 				} 				else 				{ 					if (fabs(c[1] - c[3]) > ex) 					{ 						if (fabs((c[0] - c[2]) + 2 * uc*(c[1] - c[3]) + fp2(c[1], c[3])) <= fu1)  						{ 							b1 = 1; 							b2 = 0; 							b3 = 0; 							b4 = 0; 							b5 = 0; 							b6 = 0; 							c[3] = 0.0; 						} 						else 						{ 							b1 = 0; 							b2 = 1; 							b3 = 0; 							b4 = 0; 							b5 = 0; 							b6 = 0; 							c[3] = 0.0; 						} 					} 				} 			} 			/***********(3)m1冲击plate后发生粘滞，capsule静止***********/ 			/***********(4)m1冲击plate后发生粘滞，capsule运动***********/ 			if ((c[0] - c[2]) - q >= -ex  && uk*(c[0] - c[2] - q) > 0) 			{ 				if (fabs(c[3]) > ex) 				{ 					if (fabs(c[1] - c[3]) > ex) 					{ 						b1 = 0; 						b2 = 0; 						b3 = 0; 						b4 = 0; 						b5 = 1; 						b6 = 0; 					} 					else 					{ 						if (fabs(f0*cos(w1*x + r1) + (c[2] - c[0]) + 2 * uc*(c[3] - c[1]) + uk*(c[2] - c[0] + q)) <= fu2) 						{ 							b1 = 0; 							b2 = 0; 							b3 = 0; 							b4 = 0; 							b5 = 0; 							b6 = 1; 							c[1] = c[3]; 						} 						else 						{ 							b1 = 0; 							b2 = 0; 							b3 = 0; 							b4 = 0; 							b5 = 1; 							b6 = 0; 							c[1] = c[3]; 						} 					} 				} 				else 				{ 					if (fabs(c[1] - c[3]) > ex) 					{ 						if (fabs((c[0] - c[2]) + 2 * uc*(c[1] - c[3]) + uk*(c[0] - c[2] - q) + fp2(c[1], c[3])) <= fu1) 						{ 							b1 = 0; 							b2 = 0; 							b3 = 0; 							b4 = 1; 							b5 = 0; 							b6 = 0; 							c[3] = 0.0; 						} 						else 						{ 							b1 = 0; 							b2 = 0; 							b3 = 0; 							b4 = 0; 							b5 = 1; 							b6 = 0; 							c[3] = 0.0; 						} 					} 				} 			} 			/********************************************/ 			if (m >= 200) 			{ 				fprintf(fp1, "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", w1, w1*x, c[0], c[2], c[0] - c[2], c[1] - c[3], c[3]); 			} 			if (m >= 200 && w1*x >= 30) 				break; 			/********************************************/ 			/* 			if (x >= 44) 			fprintf(fp1, "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", w1, x, c[0], c[2], c[0] - c[2], c[1] - c[3], c[3]); 			/********************************************/ 		} 	} 	fclose(fp1); 	system("pause"); }