今天我尝试做一些优化foreach声明,这可以继续XDocument.
优化前:
foreach (XElement elem in xDoc.Descendants("APSEvent").ToList())
{
//some operations
}
Run Code Online (Sandbox Code Playgroud)
优化后:
Parallel.ForEach(xDoc.Descendants("APSEvent").ToList(), elem =>
{
//same operations
});
Run Code Online (Sandbox Code Playgroud)
我看到.NET Parallel.ForEach(...)只打开一个线程!结果,时间跨度Parallel大于标准foreach.
为什么你认为.NET只开了1个线程?因为锁定文件?谢谢
我写代码:
using System.Runtime.CompilerServices;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
int i = new MyClass1() - new MyClass1();
int j = new MyClass1() + new MyClass1();
}
}
public class MyClass1
{
public static int operator -(MyClass1 i, MyClass1 j)
{
return 5;
}
[SpecialName]
public static int op_Addition(MyClass1 i, MyClass1 j)
{
return 5;
}
}
}
Run Code Online (Sandbox Code Playgroud)
编译时错误:
错误1运算符"+"无法应用于"ConsoleApplication21.MyClass1"和"ConsoleApplication21.MyClass1"类型的操作数
所以,c#编译器不喜欢行"int j = new MyClass1()+ new MyClass1();" 当我打开ILDASM时,我得到了相同的运算符过载代码:
Method #1 (06000003)
-------------------------------------------------------
MethodName: op_Subtraction (06000003)
Flags …Run Code Online (Sandbox Code Playgroud) 当我做:
public class Employee
{
public int exp;
}
class Program
{
static void Main(string[] args)
{
Employee o1 = new Employee();
o1.exp = 3;
lock (o1)
{
//I am here
}
}
}
Run Code Online (Sandbox Code Playgroud)
并获取o1的内存(地址为0x022cf940):

我意识到下面提到的几件事情:
问题:同步块的空间在哪里,我该如何找到它?"12"代表什么?
原始C#值类型,例如int是结构.那么,为什么int没有初始化?我认为应该有默认构造函数.另一方面,自定义结构是可以的.
在以下代码中
struct STRCT { }
class Program
{
static void Main(string[] args)
{
STRCT strct;
strct.Equals(8);
strct.GetHashCode();
strct.GetType();
strct.ToString();
int i;
i.Equals(8);
i.GetHashCode();
i.GetType();
i.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
从C#编译器视图中可以看到前5行代码,接下来的5行代码会生成编译错误:
使用未分配的局部变量
请解释原因?从我的观点来看,这两种类型都是结构,并且具有相同的行为.
我编写代码并得到奇怪的结果 - 整数我是8:
unsafe
{
int i = sizeof(Point);
}
Run Code Online (Sandbox Code Playgroud)
检查struct Point后,我找到了这个字段:
public bool IsEmpty { get; }
public int X { get; set; }
public int Y { get; set; }
Run Code Online (Sandbox Code Playgroud)
位数学:32 + 32 + 1 = 65位,因此> 8字节
那么,为什么sizeof返回8,而不是9?
谢谢
我使用手动 MAE 计算和 sklearn.metrics 并得到了不同的结果。为什么?
from sklearn.metrics import mean_absolute_error as MAE
cnt = 0
error = 0
len_y = len(y)
len_y_pred = len(y_pred)
if len_y == len_y_pred:
for i in range(len_y):
if y_pred[i] != y.values[i]:
cnt += 1
error += abs(y.values[i] - y_pred[i])
print('manual MAE = ', error / cnt)
# MAE from sklearn
print('sklearn MAE = ', MAE(y, y_pred))
Run Code Online (Sandbox Code Playgroud)
输出:
manual MAE = 96189.48047877151
sklearn MAE = 15074.239113119293
Run Code Online (Sandbox Code Playgroud)
为什么如此不同?
谢谢
IDE:IntelliJ、JDK 16.0.2
代码:
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
System.out.println("Free memory before gc: " + runtime.freeMemory() / (1024 * 1024) + " Mb");
runtime.gc();
System.out.println("Free memory after gc: " + runtime.freeMemory() / (1024 * 1024) + " Mb");
}
Run Code Online (Sandbox Code Playgroud)
输出:
Free memory before gc: 251 Mb
Free memory after gc: 13 Mb
Run Code Online (Sandbox Code Playgroud)
问题:为什么调用垃圾回收“吃掉”所有内存?
PSP是增量,我在冲刺结束时。对客户有价值的东西。MMF不一样吗?不过,我对客户也很有价值......
请帮我理解PSP和MMF之间的区别
谢谢