RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-193999

i-one's questions

Martin Hope
i-one
Asked: 2022-09-07 22:32:59 +0000 UTC

与循环无关的代码会减慢它的速度

  • 2

我遇到了一个不寻常的情况。在与它无关的循环之前添加的一行代码会减慢循环速度。这是“简化形式”的样子。

我有一个原始的循环方法:

int[] array = new int[ElemCnt];

public int Sum()
{
    int sum = 0;

    for (int i = 0; i < array.Length; i++)
        sum += array[i];

    return sum;
}

然后将此方法修改如下:

long state = 1;

public int Sum2()
{
    int sum = 0;

    if (Interlocked.Read(ref state) == 0)
        return sum;

    for (int i = 0; i < array.Length; i++)
        sum += array[i];

    return sum;
}

那些。它只是添加了一个循环前检查。

假设这个检查的执行时间可以通过某个常数来估计是合乎逻辑的。第二种方法(结果最差)的执行时间可以估计T2 ≈ T + C为 随着数组长度的增长,显然这两种方法的执行时间将越来越难以区分()。TCT2 ≈ T

但是,这不会发生:

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19044.1766 (21H2)
Intel Core i5-4690 CPU 3.50GHz (Haswell), 1 CPU, 4 logical and 4 physical cores
.NET SDK=6.0.301
  [Host]     : .NET 6.0.6 (6.0.622.26707), X64 RyuJIT
  DefaultJob : .NET 6.0.6 (6.0.622.26707), X64 RyuJIT
方法 元件 意思是 错误 标准差 比率 比率标准差
和 1000 590.1ns 6.22ns 5.81ns 1.00 0.00
总和2 1000 1,896.4ns 9.88ns 9.24ns 3.21 0.03
和 10000 5,729.5ns 27.40ns 24.29ns 1.00 0.00
总和2 10000 18.929.2ns 208.10ns 194.66ns 3.30 0.03
和 100000 57.273.8ns 258.89ns 242.17ns 1.00 0.00
总和2 100000 187.707.1ns 2,121.93ns 1,984.85ns 3.28 0.03
和 1000000 590.207.1ns 9,395.29ns 8,328.68ns 1.00 0.00
总和2 1000000 1,943,186.9ns 34.793.55ns 32.545.90ns 3.29 0.06

那些。它确实有效T2 ≈ K * T!

这表明循环在第二种情况下运行较慢。就好像添加的检查以某种方式减慢了循环。这是怎么回事?


基准代码:

public class LoopBench
{
    int[] array = null;
    long state = 1;

    [Params(1000, 10000, 100000, 1000000)]
    public int ElemCnt { get; set; }

    [GlobalSetup]
    public void Setup()
    {
        array = new int[ElemCnt];
    }

    [Benchmark(Baseline = true)]
    public int Sum()
    {
        int sum = 0;

        for (int i = 0; i < array.Length; i++)
            sum += array[i];

        return sum;
    }

    [Benchmark]
    public int Sum2()
    {
        int sum = 0;

        if (Interlocked.Read(ref this.state) == 0)
            return sum;

        for (int i = 0; i < array.Length; i++)
            sum += array[i];

        return sum;
    }
}
c# .net
  • 1 个回答
  • 71 Views
Martin Hope
i-one
Asked: 2020-12-13 00:51:10 +0000 UTC

Cast<T> 用于一组转换为 T 的元素

  • 5

有一个类包含从int类型到此类类型的强制转换运算符

class Item
{
    public int ID;

    public static implicit operator Item(int id)
    {
        return new Item { ID = id };
    }
}

如果你这样写

int id = 1;
Item item = id;

然后类型转换起作用。

现在我想将一组int 转换为一组Items,所以我这样做了

int[] ids = new int[] { 1, 2, 3 };
Item[] items = ids.Cast<Item>().ToArray();

并得到

System.Core.dll 中发生“System.InvalidCastException”类型的未处理异常附加信息:无法将“System.Int32”类型的对象转换为“Item”类型。

用显式替换隐式没有帮助。

解决这个问题的最优雅的方法是什么,以及(最重要的是)为什么Cast<T>不起作用?

c#
  • 2 个回答
  • 10 Views
Martin Hope
i-one
Asked: 2020-08-30 18:50:36 +0000 UTC

属性对私有常量的可见性

  • 9

让有一堂课

class Class
{
}

和属性

class SomePropertyAttribute : Attribute
{
    public SomePropertyAttribute(string prop)
    {
    }
}

为什么编译器在将属性应用于这样的类时不会抛出错误

[SomeProperty(Weird)] //<-- Weird const use
class Class
{
    private const string Weird = nameof(Weird);
}

?

实际上,在这种情况下,在定义类的花括号之外使用了一个私有常量。而文档(见私人)说:

private关键字是成员访问修饰符。
...
私有成员只能 在声明它们的类或结构的主体内访问...

c#
  • 2 个回答
  • 10 Views
Martin Hope
i-one
Asked: 2020-11-15 14:38:20 +0000 UTC

我在用这种方式阅读什么样的记忆?

  • 11

假设有一个字节数组

byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

像这样

[StructLayout(LayoutKind.Explicit)]
struct ByteToUlongConverter
{
    [FieldOffset(0)]
    public byte[] bytes;

    [FieldOffset(0)]
    public ulong[] ulongs;

    public ByteToUlongConverter(byte[] bytes)
    {
        this.ulongs = null;
        this.bytes = bytes;
    }
}

结构体。

接下来,有了这个结构,我做

ByteToUlongConverter conv = new ByteToUlongConverter(bytes);

foreach (ulong ul in conv.ulongs)
    Console.WriteLine($"{ul:X16}");

这输出类似

0807060504030201
793C086C00000000
0000000000000000
0000000000000000
0000000000000000
0000000004710B40
00000001793BFDD8
793BED0C00000000

那些。我分配了8个字节,读了64个(你甚至不仅可以读,还可以写东西)。

这些额外的字节是谁的?这是什么记忆?那些。据我所知,我的阵列位于堆中的某个地方,而这是附近的东西。.Net 中的堆是如何组织的,所有进程都一样还是每个进程都有自己的堆?那些。另一个没有特权的进程可以尝试以这种方式读取我进程中的某些内容吗?

c#
  • 2 个回答
  • 10 Views
Martin Hope
i-one
Asked: 2020-11-01 19:44:10 +0000 UTC

静态和安全

  • 1

假设有一些对象执行一些加密操作(例如KeyedHashAlgorithm, ,SymmetricAlgorithm或AsymmetricAlgorithm)。

假设一个用于散列密码的对象

KeyedHashAlgorithm keyedSha512 = KeyedHashAlgorithm.Create("HMACSHA512");

以某种方式初始化

keyedSha512.Key = ...;

要计算密码的哈希值,您可以在需要时创建并初始化一个新对象。

但是,我们假设在初始化期间,密钥是从另一台主机加载的,即 初始化可以比较长。在这种情况下,出于优化目的,要求创建和初始化一个加密对象一次,将其放置在static某个类的字段中,在应用程序运行时它将存在于其中。

它会以任何方式降低安全性吗?对此有哪些建议和最佳实践?

c#
  • 2 个回答
  • 10 Views
Martin Hope
i-one
Asked: 2020-10-07 17:25:46 +0000 UTC

ConfigSection 中元素的集合

  • 3

使用以下一组类:

public class MyConfigSection : ConfigurationSection
{
    [ConfigurationProperty("Items")]
    public ItemCollection Items
    {
        get { return ((ItemCollection)(base["Items"])); }
    }
}

[ConfigurationCollection(typeof(ItemElement), AddItemName = "Item")]
public class ItemCollection : ConfigurationElementCollection
{
    ...
}

public class ItemElement : ConfigurationElement
{
    ...
}

我把配置部分的阅读整理成如下形式:

<MySection>
  <Items>
    <Item ... />
    <Item ... />
    <Item ... />
  </Items>
</MySection>

如果我希望该部分看起来像,是否可以以某种方式设置阅读

<MySection>
  <Item ... />
  <Item ... />
  <Item ... />
</MySection>

那些。这样集合的元素就不会被包含在 中<Items></Items>,而是直接从节中读取。

c#
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5