[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] Speed comparison of R and C
From: |
Gábor Csárdi |
Subject: |
Re: [igraph] Speed comparison of R and C |
Date: |
Wed, 9 Feb 2011 00:05:46 +0100 |
On Tue, Feb 8, 2011 at 9:25 PM, Tamás Nepusz <address@hidden> wrote:
> Hi,
>
> Here's an explanation about what's going on behind the scenes. Basically, g
> <- set.edge.attribute(g, "cap", index=x, value=x) makes a *copy* of the graph
> object in every iteration of the for loop; set.edge.attribute always copies
> the graph first, applies the modification to the copy and assigns it back to
> g. I conjecture that this operation causes most of the overhead. As I'm not
> too familiar with R, I don't know why it works this way -- maybe Gabor can
> shed light on it.
Very simple explanation. This is how R works. Not just with igraph
graphs, but with (almost) every other object as well. Functions don't
modify their arguments.
Of course it is possible to circumvent this, but that does not really
make sense, because then igraph would behave VERY differently than all
other R code.
So, yeah, igraph is not too good if your graph changes a lot. Not just
attributes, but deleting/adding vertices/edges individually is also
relatively slow.
Gabor
> However, depending on your use-case, there might be more efficient ways of
> doing things in R. For instance, your current for loop basically retrieves an
> edge attribute once for every edge. Instead of that, you could simply write
> E(g)$load to retrieve the values of the "load" edge attribute for all the
> edges at once, and this is much faster. Similarly, you can use E(g)$cap <-
> capacities to assign to all the edge attributes at once, and this is also
> much faster.
>
> --
> Tamas
>
>
> On 8 Feb 2011, at 20:38, William Tu wrote:
>
>> Sorry guys,
>>
>> My updated rw_speed.R.
>>
>> William
>>
>> On Tue, Feb 8, 2011 at 2:24 PM, William Tu <address@hidden> wrote:
>>> Hi Tamas,
>>>
>>> Thanks for all the advices. I've attached my C and R code.
>>>
>>> btw, is this the correct way to reply to the mailing list? I click the
>>> "reply via email to" button below the discussion page.
>>>
>>> William
>>>
>>>
>> <rw_speed.R>
>
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>
--
Gabor Csardi <address@hidden> UNIL DGM
RE: [igraph] Speed comparison of R and C, cheng-chun Tu, 2011/02/11