小编zzf*_*ima的帖子

为什么Parallel.ForEach不运行多个线程?

今天我尝试做一些优化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个线程?因为锁定文件?谢谢

c# parallel-processing

22
推荐指数
1
解决办法
2万
查看次数

为什么C#编译器生成错误,即使使用属性"SpecialName"

我写代码:

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)

c# attributes operator-overloading

7
推荐指数
1
解决办法
98
查看次数

CLR同步块地址

当我做:

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):

在此输入图像描述

我意识到下面提到的几件事情:

  1. 绿色矩形是同步块,即12
  2. 蓝色矩形是4字节类型的地址
  3. 红色矩形是4字节整数,即3;

问题:同步块的空间在哪里,我该如何找到它?"12"代表什么?

c# heap clr

6
推荐指数
1
解决办法
1164
查看次数

使用未分配的局部变量:值类型与自定义结构

原始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行代码会生成编译错误:

使用未分配的局部变量

请解释原因?从我的观点来看,这两种类型都是结构,并且具有相同的行为.

c# struct

4
推荐指数
2
解决办法
503
查看次数

为什么sizeof(Point)是8?

我编写代码并得到奇怪的结果 - 整数我是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?

谢谢

.net c# sizeof

3
推荐指数
2
解决办法
223
查看次数

sklearn.metricsmean_absolute_error计算

我使用手动 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)

为什么如此不同?

谢谢

python scikit-learn

3
推荐指数
1
解决办法
9999
查看次数

Java:调用 GC 会占用所有可用内存

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)

问题:为什么调用垃圾回收“吃掉”所有内存?

java garbage-collection

1
推荐指数
1
解决办法
64
查看次数

潜在可交付产品 (PSP) 和最小适销功能 (MMF) 之间的差异

PSP是增量,我在冲刺结束时。对客户有价值的东西。MMF不一样吗?不过,我对客户也很有价值......

请帮我理解PSP和MMF之间的区别

谢谢

agile scrum

0
推荐指数
1
解决办法
3385
查看次数