Skip to content

Suggestion for improving performance #23

@tealloovvee

Description

@tealloovvee

Hi, I read your code and would like to suggest some minor edits to improve performance:

  1. Self weight can be written directly into a vector

    for (size_t v = 0; v < n; v++)
    {
    #ifdef DEBUG
    cerr << "\t" << "Size node " << v << ": " << this->node_size(v) << endl;
    #endif
    double self_weight = 0.0;
    // There should be only one self loop
    igraph_integer_t eid;
    // Get edge id for self loop
    igraph_get_eid(this->_graph, &eid, v, v, this->is_directed(), false);
    if (eid >= 0)
    self_weight = this->edge_weight(eid);
    this->_node_self_weights[v] = self_weight;
    #ifdef DEBUG
    cerr << "\t" << "Self weight node " << v << ": " << self_weight << endl;
    #endif
    }

  2. The graph type check can be move outside the loop

    for (size_t e = 0; e < m; e++) {
    double w = this->edge_weight(e);
    this->_total_weight += w;
    size_t from, to;
    this->edge(e, from, to);
    if (this->is_directed()) {
    this->_strength_in[to] += w;
    this->_strength_out[from] += w;
    this->_degree_in[to]++;
    this->_degree_out[from]++;
    this->_degree_all[to]++;
    this->_degree_all[from]++;
    } else {
    // we only compute strength_in and degree_in for undirected graphs
    this->_strength_in[to] += w;
    this->_strength_in[from] += w;
    // recall that igraph ignores the mode for undirected graphs
    this->_degree_in[to]++;
    this->_degree_in[from]++;
    }
    }

    if (from == to && !this->is_directed())
    w /= 2.0;

  3. for each v_community, we reinit an empty vector. Two ideas: move this vector outside loop + clear after the iteration (capacity not changed); add vector reserve if we can simple find realistic upper bound for number of records

    for (size_t v_comm = 0; v_comm < n_collapsed; v_comm++) {
    vector<size_t> neighbour_communities;
    for (size_t v : community_memberships[v_comm]) {
    for (size_t e : this->get_neighbour_edges(v, IGRAPH_OUT)) {
    size_t from, to;
    this->edge(e, from, to);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions