简单代码的性能差异

《.net 框架程序设计》里的几句代码,我重新整理了一下,写成函数,这样下面的反编译代码看起来更方便点:

public   void  Boxing()
        
{
            Int32 v 
= 5;
            Console.WriteLine(
"{0},{1},{2}",v,v,v);
        }

        
public   void  Boxing1()
        
{
            Int32 v 
= 5;
            
object o = v;
            Console.WriteLine(
"{0},{1},{2}",o,o,o);
        }
两个函数的输出结果是一样的,但是哪一个函数更好呢?

第一个显然更简单,但是在性能上第二个好,看看反编译的代码:
Boxing函数:
.method  public  hidebysig instance  void  Boxing() cil managed
{
      
// Code Size: 31 byte(s)
      .maxstack 4
      .locals (
            int32 V_0)
      L_0000: ldc.i4.
5 
      L_0001: stloc.
0 
      L_0002: ldstr 
"{0},{1},{2}"
      L_0007: ldloc.
0 
      L_0008: box int32
      L_000d: ldloc.
0 
      L_000e: box int32
      L_0013: ldloc.
0 
      L_0014: box int32
      L_0019: call 
void [mscorlib]System.Console::WriteLine(stringobjectobjectobject)
      L_001e: ret 
}


 

Boxing1函数的:
.method  public  hidebysig instance  void  Boxing1() cil managed
{
      
// Code Size: 23 byte(s)
      .maxstack 4
      .locals (
            int32 V_0,
            
object V_1)
      L_0000: ldc.i4.
5 
      L_0001: stloc.
0 
      L_0002: ldloc.
0 
      L_0003: box int32
      L_0008: stloc.
1 
      L_0009: ldstr 
"{0},{1},{2}"
      L_000e: ldloc.
1 
      L_000f: ldloc.
1 
      L_0010: ldloc.
1 
      L_0011: call 
void [mscorlib]System.Console::WriteLine(stringobjectobjectobject)
      L_0016: ret 
}



第一个函数有三次装箱操作,第二个只有一次,在内存和时间消耗上都要小些。

这么简单的代码,在性能上都有这么大的差异,以前写的程序里还不知道有多少有问题的代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Epoll和select都是用于监听网络文件描述符事件的系统调用,在高并发场景下,Epoll通常被认为比select有更高的性能。这是因为Epoll是专门为处理大量连接设计的,它允许一次性添加、修改和删除多个描述符,而无需像select那样逐个检查每个描述符。 下面是一个简单的C++代码示例,展示了如何使用epoll和select来进行性能测试对比: ```cpp #include <iostream> #include <vector> #include <unistd.h> #include <sys/eventfd.h> #include <fcntl.h> #include <time.h> // 创建模拟的大量连接 void create_connections(int n, std::vector<int>& descriptors) { for (int i = 0; i < n; ++i) { int fd = eventfd(0, EFD_NONBLOCK); if (fd >= 0) { descriptors.push_back(fd); } else { perror("eventfd failed"); } } } // 使用epoll模拟读取事件 void poll_with_epoll(int descriptor, bool& done) { struct epoll_event ev; ev.events = EPOLLIN | EPOLLET; ev.data.fd = descriptor; int epoll_fd = epoll_create1(0); if (epoll_fd == -1) { perror("epoll_create1 failed"); return; } while (!done) { epoll_wait(epoll_fd, &ev, 1, -1); // 监听事件 } close(epoll_fd); } // 使用select模拟读取事件 void poll_with_select(int descriptor, bool& done) { fd_set read_fds; FD_ZERO(&read_fds); FD_SET(descriptor, &read_fds); timeval tv; tv.tv_sec = TV_SEC; // 设置超时时间 while (!done) { int ret = select(descriptor + 1, &read_fds, nullptr, nullptr, &tv); if (ret == -1 && errno != EINTR) { perror("select failed"); break; } else if (ret > 0) { done = true; // 检测到事件就结束 } } } int main() { int num_descriptors = 1000; // 模拟大量连接 std::vector<int> descriptors; create_connections(num_descriptors, descriptors); // 并行运行两个测试 bool done_epoll = false, done_select = false; pthread_t epoll_thread, select_thread; pthread_create(&epoll_thread, nullptr, &poll_with_epoll, std::ref(descriptors), std::ref(done_epoll)); pthread_create(&select_thread, nullptr, &poll_with_select, std::ref(descriptors), std::ref(done_select)); // 等待其中一个完成 pthread_join(epoll_thread, nullptr); pthread_join(select_thread, nullptr); return 0; } ``` 这个程序会创建大量模拟连接,并启动两个线程分别使用epoll和select监控这些连接。通过测量它们完成的时间,可以对比两者的性能差异。注意这只是一个基本的测试框架,实际测试时需要考虑更多的因素如系统负载、硬件条件等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值