小编Joh*_*nes的帖子

"最佳"IO缓冲 - 程序员或内核的任务?

我的任务很简单:在Linux上用C++读取和解析一个大文件.有两种方法:

  1. 逐字节解析.

    while(/*...*/) {
            ... = fgetc(...);
            /* do something with the char */
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 缓冲区解析缓冲区.

    while(/*...*/) {
            char buffer[SOME_LARGE_NUMBER];
            fread(buffer, SOME_LARGE_NUMBER, 1, ...);
            /* parse the buffer */
    }
    
    Run Code Online (Sandbox Code Playgroud)

现在,逐字节解析对我来说更容易(不检查​​缓冲区的满载程度等).但是,我听说读大片更有效率.

什么是哲学?是"最佳"缓冲内核的任务,所以当我打电话时它已经被缓冲了fgetc()?或者是否建议我处理它以获得最佳效率?

此外,除了所有哲学:Linux上的现实是什么?

c++ io performance buffer kernel

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

C++的朋友函数隐藏了类函数?

最小的例子:

class A
{
    friend void swap(A& first, A& second) {}
    void swap(A& other) {}
    void call_swap(A& other)
    {
        swap(*this, other);
    }
};

int main() { return 0; }
Run Code Online (Sandbox Code Playgroud)

g ++ 4.7说:

friend.cpp: In member function ‘void A::call_swap(A&)’:
friend.cpp:7:20: error: no matching function for call to ‘A::swap(A&, A&)’
friend.cpp:7:20: note: candidate is:
friend.cpp:4:7: note: void A::swap(A&)
friend.cpp:4:7: note:   candidate expects 1 argument, 2 provided
Run Code Online (Sandbox Code Playgroud)

退出第4行:

// void swap(A& other) {}
Run Code Online (Sandbox Code Playgroud)

......它工作正常.如果我想保留交换功能的两种变体,为什么以及如何解决这个问题?

c++ friend friend-function

10
推荐指数
3
解决办法
1688
查看次数

在gnuplot的plot命令中进行计算

以下gnuplot代码工作正常:

plot 'data.txt'  using 2:4  '   %lf %lf %lf %lf' title "1 PE" with linespoints;
Run Code Online (Sandbox Code Playgroud)

在下面的代码中,我想说:"使用第4列中的数字,然后将其除以第3列中的数字".或者:"使用第2列中的数字,但将其除以常数2.0".以下代码演示了我尝试实现的内容,但它不起作用.

plot 'data.txt'  using 2:4/4  '   %lf %lf %lf %lf' title "1 PE" with linespoints;
plot 'data.txt'  using 2:4/2.0  '   %lf %lf %lf %lf' title "1 PE" with linespoints;
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?

gnuplot

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

需要一个有效的减法算法模数

对于给定的数字x,y并且n,我想计算x-y mod n在C.在这个例子来看一下:

int substract_modulu(int x, int y, int n)
{
    return (x-y) % n;
}
Run Code Online (Sandbox Code Playgroud)

只要x>y,我们没事.然而,在另一种情况下,modulu操作是未定义的.

你可以想到x,y,n>0.我希望结果是正的,所以如果(x-y)<0,那么((x-y)-substract_modulu(x,y,n))/ n应该是一个整数.

您知道的最快算法是什么?有没有避免任何电话ifoperator?

c c++ math modulus

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

在C++ 11中移出std priority_queue的元素

最小的工作示例.

#include <cassert>
#include <list>
#include <queue>
//#define USE_PQ

struct MyClass
{
    const char* str;
    MyClass(const char* _str) : str(_str) {}
    MyClass(MyClass&& src) { str = src.str; src.str = nullptr; }
    MyClass(const MyClass&) = delete;
};

struct cmp_func
{
    bool operator() (const MyClass&, const MyClass&) const
    {
        return true;
    }
};

typedef std::priority_queue<MyClass, std::vector<MyClass>, cmp_func> pq_type;

#ifdef USE_PQ
MyClass remove_front(pq_type& l)
{
    MyClass moved = std::move(l.top());
    // error from the above line:
    // use of deleted function ‘MyClass::MyClass(const MyClass&)’
    l.pop(); …
Run Code Online (Sandbox Code Playgroud)

c++ move-constructor move-semantics c++11

8
推荐指数
4
解决办法
4163
查看次数

可以返回一个括起来的封闭初始化器导致C++中的副本吗?

例:

struct s { int a; };

s func() { return {42}; }

int main() {
    s new_obj = func(); // line 6
    (void) new_obj;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这有效.现在,如果我们假设我们的编译器没有RVO会发生什么?

  1. func返回一个struct的结构s,因此{42}必须转换为s,然后返回并最终复制到new_obj第6行.
  2. func 返回初始化列表,因此无法进行深层复制.

语言是什么意思?你能举一个证明吗?

注意:我知道这在这个例子中看起来没用,但是对于返回非常大的常量std::arrays,我不想依赖RVO.

c++ initializer-list return-value-optimization c++11

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

在C++ 11中构造一个联合元组

元组有点像结构.是否还有像工会一样的元组?或者我可以在元组中访问成员的工会,例如

my_union_tuple<int, char> u;
get<1>(u);
get<int>(u); // C++14 only, or see below
Run Code Online (Sandbox Code Playgroud)

对于第二行,请参见此处.

当然,解决方案不仅适用于特定的联合<int, char>,而且适用于任意类型和数量的类型.

c++ tuples unions c++11

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

将std :: vector重命名为另一个类进行重载?

看看这段代码.

#include <vector>

template<class ...Args>
using other_vector = std::vector<Args...>;

template<class T>
void f(std::vector<T>& ) {}
template<class T>
void f(other_vector<T>& ) {}

int main()
{
    other_vector<int> b;
    f(b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它没有编译,因为f正在重新声明.我完全理解错误.但是,我需要一个行为类似的第二个类std::vector<T>,但将被视为一个不同的类型,因此重载,如上例所示,是合法的.

我能做什么?

  • 让新类std::vector<T>作为基类.这可能有效,但不应该从std容器继承.
  • 让新类具有std :: vector类型的成员,然后重新声明所有函数以重定向到成员的函数.听起来很多工作.

有更好的选择吗?允许使用C++ 11或C++ 14.

c++ stdvector c++11

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

将C++ 11枚举类作为模板传递,同时自动推断其类型

这个最小的例子编译没有警告和运行:

// library
template<class T, T t> struct library_struct {};

// user
enum class my_enum { x, y, z };
int main()
{
    library_struct<my_enum, my_enum::x> unused; // l.7
    (void) unused;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,我希望编译器my_enum从枚举模板参数中推导出类型模板参数my_enum::x.这样看起来会更好:

library_struct<my_enum::x> unused;
Run Code Online (Sandbox Code Playgroud)

我见过编译器能够推导出模板参数的例子,但我只允许省略模板参数列表中的最后一个模板参数.那么可以在这里省略枚举类型吗?

编辑:我对没有宏的解决方案感兴趣.

c++ enums templates c++11

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

Java编写互斥锁的最快方法?

互斥体在许多编程语言中很常见,例如C/C++.我想念他们用Java.但是,我可以通过多种方式编写自己的方法class Mutex:

  • 使用简单的synchronized关键字Mutex.
  • 使用二进制信号量.
  • 使用原子变量,比如讨论在这里.
  • ...?

什么是最快(最好的运行时)方式?我认为同步是最常见的,但性能呢?

java performance multithreading mutex

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